Ubuntu ld 找不到 lRcpp

Ubuntu ld can not find lRcpp

我通过 r-cran-rcpp 安装了 Rcpp,我使用 dpkg -L r-cran-rcpp 发现

ls /usr/lib/R/site-library/Rcpp/libs/
Rcpp.so

但是当我使用 ld -L /usr/lib/R/site-library/Rcpp/libs -l Rcpp ld 时抱怨找不到 Rcpp,知道发生了什么事吗?

事实上,我正在使用 Seamless R 和 C++ 的第一个示例 与Rcpp集成,但以下代码抱怨找不到lrcpp

首先是fib.cpp中的cpp代码

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int fibonacci(const int x) {

if (x == 0) return(0);

 if (x == 1) return(1);

return (fibonacci(x - 1)) + fibonacci(x - 2);

 }

extern "C" SEXP fibWrapper(SEXP xs) {

int x = Rcpp::as<int>(xs);

 int fib = fibonacci(x);

return (Rcpp::wrap(fib));

 }

~然后我试着编译它们

 PKG_CXXFLAGS="-I/home/sunxd/R/x86_64-pc-linux-gnu-library/3.3/Rcpp/include" \
 PKG_LIBS="-L/home/sunxd/R/x86_64-pc-linux-gnu-library/3.3/Rcpp/libs -lRcpp" \
R CMD SHLIB fib.cpp

PKG_CXXFLAGS="-I/usr/lib/R/site-library/Rcpp/include" \
 PKG_LIBS="-L/usr/lib/R/site-library/Rcpp/libs -lRcpp" \
R CMD SHLIB fib.cpp

好的,现在我们有了一个完整的问题并且可以重现,我们可以解决这个问题:

  1. OP 尝试重现我 2013 年书中的介绍示例
  2. 这本书出版后,我们仍然链接反对 Rcpp。
  3. 不久之后,情况发生了变化,我们现在使用的东西结合了 仅 headers 加上在加载时实例化(而不是链接)
  4. 这样链接步骤就多余了。
  5. 如果您将示例更新为空(或未设置)PKG_LIBS 那么一切都很好:

完整示例:

/tmp$ cat fibonacci.cpp 

#include <Rcpp.h>

int fibonacci(const int x) {
   if (x == 0) return(0);
   if (x == 1) return(1);
   return (fibonacci(x - 1)) + fibonacci(x - 2);
}

extern "C" SEXP fibWrapper(SEXP xs) {
   int x = Rcpp::as<int>(xs);
   int fib = fibonacci(x);
   return (Rcpp::wrap(fib));
}
/tmp$ cat rcpp.sh
#!/bin/sh
PKG_CXXFLAGS="-I/usr/local/lib/R/site-library/Rcpp/include" \
PKG_LIBS="" \
R CMD SHLIB fibonacci.cpp
/tmp$ ./rcpp.sh 
g++ -I/usr/share/R/include -DNDEBUG     -I/usr/local/lib/R/site-library/Rcpp/include -fpic  -g -O3 -Wall -pipe -Wno-unused -pedantic -c fibonacci.cpp -o fibonacci.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o fibonacci.so fibonacci.o -L/usr/lib/R/lib -lR
/tmp$ 

您现在可以加载 fibonacci.so

如今,Rcpp 属性 好多了。看到它的小插图。