在Linux上使用g ++编译RInside程序

时间:2012-07-18 08:34:15

标签: linux r g++ rcpp rinside

我已经通过RPM安装了R(使用Yast)。

anisha@linux-y3pi:~> locate RInside.so
/usr/lib64/R/library/RInside/lib/libRInside.so
/usr/lib64/R/library/RInside/libs/RInside.so

anisha@linux-y3pi:~> locate Rcpp.so
/usr/lib64/R/library/Rcpp/lib/libRcpp.so
/usr/lib64/R/library/Rcpp/libs/Rcpp.so

我想编译的文件(rinsidetest.cpp)包含:

#include <RInside.h>
#include <Rcpp.h>

int main (int argc, char *argv[])
{
    RInside R (argc, argv);
    return 0;
}

以下是我编译的方法:

anisha@linux-y3pi:~> g++ rinsidetest.cpp -I /usr/lib64/R/library/RInside/include/ -I /usr/lib64/R/library/Rcpp/include/ -I /usr/lib64/R/include/ -L /usr/lib64/R/library/RInside/libs/ -L /usr/lib64/R/library/Rcpp/libs/ 

/tmp/ccUfdjgZ.o: In function `main':  
rinsidetest.cpp:(.text+0xb7): undefined reference to `RInside::RInside(int, char const* const*, bool)'  
rinsidetest.cpp:(.text+0xc8): undefined reference to `RInside::~RInside()'  
collect2: ld returned 1 exit status

R信息:

> sessionInfo() 
R version 2.15.1 (2012-06-22)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=C                 LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] tcltk_2.15.1 tools_2.15.1
> 

RInside版本:0.2.6
Rcpp版本:0.9.13

是否有针对此问题的解决方法,或者我“必须”删除rpm安装并通过从源代码编译启动所有内容?

修改

anisha@linux-y3pi:~/Desktop/abc> g++ rinsidetest.cpp -I /usr/lib64/R/library/RInside/include/ -I /usr/lib64/R/library/Rcpp/include/ -I /usr/lib64/R/include/ -L /usr/lib64/R/library/RInside/lib/ -L /usr/lib64/R/library/Rcpp/lib/ -lRInside

anisha@linux-y3pi:~/Desktop/abc> ls  
a.out  rinsidetest.cpp

anisha@linux-y3pi:~/Desktop/abc> ./a.out   
./a.out: error while loading shared libraries: libRInside.so: cannot open shared object file: No such file or directory

2 个答案:

答案 0 :(得分:2)

尝试将以下内容添加到g ++构建的末尾

 -lRInside -lRcpp

所以你有

g++ rinsidetest.cpp -I /usr/lib64/R/library/RInside/include/ -I /usr/lib64/R/library/Rcpp/include/ -I /usr/lib64/R/include/ -L /usr/lib64/R/library/RInside/libs/ -L /usr/lib64/R/library/Rcpp/libs/ -lRInside -lRcpp

目前看来,您似乎没有链接到您需要的库。如果您有一个名为libXYZ的库,则需要使用带有-lXYZ的g ++链接到它,您似乎还没有这样做。这可能会解决问题

答案 1 :(得分:2)

如果删除新的测试文件

rinsidetest.cpp

进入包含准备好的Makefile的目录 - 例如examples/standard/,那么你只需要说

make

将构建正确的可执行文件。它通过嵌入目录来处理运行时链接问题。

这个想法是让用户使用提供的Makefile,并根据需要进行调整/复制。