Compiling RInside program with g++ on Linux

2019-05-31 01:01发布

问题:

I have installed R through RPM (with 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

The file (rinsidetest.cpp) that I wish to compile contains:

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

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

Here's how I compile it:

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 information:

> 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 version: 0.2.6
Rcpp version: 0.9.13

Is there a workaround for this problem, or I "have to" remove the rpm installation and start everything by compiling from source?

EDIT

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

回答1:

Try adding the following to the end of your g++ build

 -lRInside -lRcpp

so that you have

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

As it stands it doesnt appear that you are linking to the library you need. When you have a library named libXYZ you need to link to it using g++ with -lXYZ which it appears you have not done. This may fix the problem



回答2:

If you drop your new test file

rinsidetest.cpp

into a directory containing the prepared Makefile -- as eg examples/standard/, then you only need to say

make

and the correct executable will be built. It takes care of the run-time linking issue by embedding the directory.

The idea is for users to take the provided Makefile, and adapt/copy as needed.