我最近下载了2013 RCPP书断亚马逊学习如何更好地使用C++和我的R代码里面,我想用第一斐波那契的递归函数和包装的第一个编译例子,看看我能做到它。我在Ubuntu了最新的R.RCPP一次编译麻烦
首先我的C++:
/* Cpp based recurive function */
int fibonacci(const int x){
if(x == 0) return(0);
if(x == 1) return(1);
return(fibonacci(x - 1) + fibonacci(x - 2));
}
/* Wrapper */
extern "C" SEXP fibWrapper(SEXP xs) {
int x = Rcpp::as<int>(xs);
int fib = fibonacci(x);
return(Rcpp::wrap(fib));
}
然后我开始sh和输入:
PKG_CXXFLAGS=`Rscript -e 'Rcpp:::CxxFlags()'`
PKG_LIBS=`Rscript -e 'Rcpp:::LdFlags()'`
R CMD SHLIB Fibonacci.cpp
,但我得到:
g++ -I/usr/share/R/include -DNDEBUG -fpic -O3 -pipe -g -c Fibonacci.cpp -o Fibonacci.o
Fibbonacci.cpp:10:12: error: 'SEXP' does not name a type
make: *** [Fibonacci.o] Error 1
我想也许我需要包含在我的C++代码中的指令,所以我再次执行它,但是这次在C++文件的顶部有#include<Rcpp.h>
,并再次做同样的命令,但仍然没有快乐:
g++ -I/usr/share/R/include -DNDEBUG -fpic -O3 -pipe -g -c Fibonacci.cpp -o Fibonacci.o
Fibonacci.cpp:1:18: fatal error: Rcpp.h: No such file or directory
compilation terminated.
make: *** [Fibbonacci.o] Error 1
我做了什么错了? 如果我查询我在SH设定值:
$PKG_CXXFLAGS
sh: 9: -I/local/yrq12edu/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/include: not found
$PKG_LIBS
sh: 10: -L/local/yrq12edu/R/x86_64-pc-linux-gnu-library/3.0/Rcpp/lib: not found
但是我觉得没有找到的消息都只是因为-L标志,因为这些文件存在,如果我到该目录。
感谢, 奔。
我对此并不熟悉,大概是这样的:'Rcpp ::: SHLIB(“fibbonacci.cpp”)'在包含cpp文件的目录中? – Ward9250
刚刚尝试过使用Rcpp ::: SHLIB,因为您和本博客的建议是这样的:http://www.computerbastard.com/rcpp-c-compilation-trick/我收到以下内容:'$ Rscript -e“ Rcpp ::: SHLIB('fibbonacci.cpp')“ make:***目标'fibbonacci.o'没有规则,需要'fibbonacci.so'。停止' – Ward9250
对于它的价值,我完全不同意Rcpp :: SHLIB的建议。它可能在这里被删除。 –