c++linkerincludercpprinside

How can I get the linkers to work when I include the RInside header in a C++ program?


I am trying to compile the examples in the standard examples repository of the RInside package. I have already tried running the Makefile that was already in the repository, didn't work. Until now I've run into several errors, some of which have been solved so far. The problem is that every time I solve an error one of two things happen, it appears a new error or an old one reappears. This is the code of the different makefiles with the respective error:

1)Makefile without the -L

    all:
        g++ -I/usr/share/R/include -I/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include -I/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/RInside/include rinsidetest.cpp

as expected, applying make to this file returns errors about undefined references to RInside::'s

undefined reference to `RInside::RInside(int, char const* const*, bool, bool, bool)'

2)Makefile with the -L links

all:
g++ -I/usr/share/R/include -I/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/include -I/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/RInside/include -L/usr/lib/R/site-library/RInside/lib -L/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/libs -lRInside -lRcpp -L/usr/lib/R/lib -lR -Wl,-rpath,/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/Rcpp/libs -lRInside -Wl,-rpath,/home/manuel/R/x86_64-pc-linux-gnu-library/3.4/RInside/lib rinsidetest.cpp

The error was this:

/usr/bin/ld: cannot find -lRcpp

it turns out that the .so file in the libs repository was named Rcpp.so, so I renamed it as libRcpp.so and the error was gone.

3)After renaming Rcpp.so as libRcpp.so I applied make to the same makefile of point 2) and the errors were once again about undefined references to RInside:::

rinsidetest.cpp:(.text+0x100): undefined reference to `RInside::RInside(int, char const* const*, bool, bool, bool)

4)Applying make to the makefile that was already in the folder

make -f Makefile

the error (after changing R_LIBS_USER to

R_LIBS_USER :=      "/home/manuel/R/x86_64-pc-linux-gnu-library/3.4"

was:

fatal error: RInside.h: No such file or directory compilation terminated.

So far I've read more general questions about including headers in C++, and a couple of questions specifically about RInside, some of which were answered by Dirk Eddelbuettel, who wrote the package, but all of the answers were related to the use of the linkers -L<path>/include which I'm almost sure I'm using correctly.

This are some of the questions I've read until now:

https://stackoverflow.com/questions/11537313/compiling-rinside-program-with-g-on-linux[https://stackoverflow.com/questions/11537313/compiling-rinside-program-with-g-on-linux][1]

Compiling the Rcpp package

http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2012-May/003829.html

http://rcpp-devel.r-forge.r-project.narkive.com/A70U2nVw/problem-with-rinside-hello-world-example

I'm using Ubuntu 16.04 with R version 3.4.4. Any help will be appreciated!


Solution

  • I am not sure where you got lost but the basic idea is to just say this:

    1. Change into the directory.
    2. make clean just in case.
    3. make (which is make all) or just one.

    and that still works---I use Debian/Ubuntu myself.

    Here is do make rinside_sample0 to prove the point, and then run it:

    edd@rob:~$ cd git/rinside/inst/examples/standard/
    edd@rob:~/git/rinside/inst/examples/standard$ make -f GNUmakefile clean
    rm -vf rinside_sample9 [....stuff remove to keep it shorter...]  rinside_sample16
    rm -vrf *.dSYM
    edd@rob:~/git/rinside/inst/examples/standard$ make -f GNUmakefile rinside_sample0
    ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include \
               -I/usr/local/lib/R/site-library/RInside/include -g -O3 -Wall -pipe \
               -Wno-misleading-indentation -Wno-unused \
               -Wno-ignored-attributes -Wno-deprecated-declarations \
               -march=native -Wall  rinside_sample0.cpp  -Wl,--export-dynamic \
               -fopenmp -Wl,-Bsymbolic-functions -Wl,-z,relro -L/usr/lib/R/lib -lR \
               -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -licuuc -licui18n \
               -lblas -llapack  -L/usr/local/lib/R/site-library/RInside/lib \
               -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib \
               -o rinside_sample0
    edd@rob:~/git/rinside/inst/examples/standard$ ./rinside_sample0 s
    Hello, world!
    edd@rob:~/git/rinside/inst/examples/standard(master)$ 
    

    I indented this by hand, and I may have some local settings in ~/.R/Makevars which show up here -- that does not matter.

    What matters is that it works out of the box if you just leave it alone. If you change the setup and it breaks you get to solve a problem you didn't have to create in the first place.