3

I would like to use RInside in a c++x11 code to call R routine. By default, R seems to install a c++98 version of the library and thus the linking does not success. I wonder what are the different steps I should proceed to install c++x11 version of RInside (but I guess that I need to recompile all R/R package, is it ?) and if some more simple solution exists. Thanks in advance (I work on MacOs)

Update: I reinstalled Rcpp and RInside doing (my Makevars is empty so c++98 version)

 sudo R CMD INSTALL ~/Downloads/Rcpp_0.10.4.tar.gz 
 sudo R CMD INSTALL ~/Downloads/RInside_0.2.10.tar.gz

Then I compile the hello world example using (no cx11) :

clang++ -I/Library/Frameworks/R.framework/Versions/2.15/Headers/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/include/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/  -c ../src/Gui/test.cc -o testOut.cc.o

clang++ testOut.cc.o -o testOut -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/lib/x86_64 -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/lib/x86_64 -framework R -lRInside -lRcpp

That produces me:

  Hello, world!

However, by adding the x11 option:

clang++ -I/Library/Frameworks/R.framework/Versions/2.15/Headers/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/include/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/  -c ../src/Gui/test.cc -o testOut.cc.o -stdlib=libc++ -std=c++11

clang++ testOut.cc.o -o testOut -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/lib/x86_64 -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/lib/x86_64 -framework R -lRInside -lRcpp -stdlib=libc++ -std=c++11

I get (when linking):

  Undefined symbols for architecture x86_64:
   "RInside::parseEvalQ(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
  _main in testOut.cc.o
   "RInside::operator[](std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
  _main in testOut.cc.o
   "Rcpp::Environment::assign(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, SEXPREC*) const", referenced from:
        bool Rcpp::Environment::assign<char [15]>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, char const (&) [15]) const in testOut.cc.o

When recompiling Rcpp and RInside using c++x11 options

clang++ -I/Library/Frameworks/R.framework/Versions/2.15/Headers/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/include/ -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/  -c ../src/Gui/test.cc -o testOut.cc.o -stdlib=libc++ -std=c++11

clang++ testOut.cc.o -o testOut -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/lib/x86_64 -L/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/lib/x86_64 -framework R -lRInside -lRcpp -stdlib=libc++ -std=c++11

liking is ok but ./test gives me a seg fault.

Add: my clang version is : Apple clang version 4.1 (tags/Apple/clang-421.11.66)

beuhbbb
  • 257
  • 3
  • 14
  • I would say that you did not "successfully" build the library if your program goes belly-up on the simplest example. There is also no real need to re-print the source code which has been in the RInside repo and distribution for years. – Dirk Eddelbuettel Oct 16 '13 at 13:45

2 Answers2

2

R is compiled with a c compiler. You don't necessarily need to use the same to build packages. So it is perfectly valid to use clang to compile add on packages. I have this in my ~/.R/Makevars file to use clang and C++11 for package:

CC=clang
CXX=clang++
CXXFLAGS= -stdlib=libc++ -std=c++11

Also, you might want to have a look at Rcpp11, a C++11 centric redesign of Rcpp. It probably would not be too hard to fork RInside to use Rcpp11 instead of Rcpp.

Romain Francois
  • 17,432
  • 3
  • 51
  • 77
0

I am not aware of different linker requirements. What you have should just work. Otherwise please show your actual linking steps and errors, preferably in a reproducible way I could look at at my end too.

Witness the following. We first build with default flags:

edd@max:~/svn/rinside/pkg/inst/examples/standard$ grep ^CXXFLAGS ~/.R/Makevars
CXXFLAGS=             -g -O3 -Wall -pipe -Wno-unused -pedantic    # plain C++ 
edd@max:~/svn/rinside/pkg/inst/examples/standard$ make rinside_sample0
ccache g++-4.7 -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-unused -pedantic -Wall   \
  rinside_sample0.cpp  -L/usr/lib/R/lib -lR  -lblas \
  -llapack -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp \
  -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib \
  -L/usr/local/lib/R/site-library/RInside/lib \
  -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib \
  -o rinside_sample0
edd@max:~/svn/rinside/pkg/inst/examples/standard$ ./rinside_sample0 
Hello, world!       
edd@max:~/svn/rinside/pkg/inst/examples/standard$ 

Now I am simply adding -std=c++11 to my CXXFLAGS:

edd@max:~/svn/rinside/pkg/inst/examples/standard$ grep ^CXXFLAGS ~/.R/Makevars 
CXXFLAGS=             -g -O3 -Wall -pipe -Wno-unused -pedantic -std=c++11
edd@max:~/svn/rinside/pkg/inst/examples/standard$  

and remake the binary:

edd@max:~/svn/rinside/pkg/inst/examples/standard$ rm rinside_sample0  
edd@max:~/svn/rinside/pkg/inst/examples/standard$ make rinside_sample0
ccache g++-4.7 -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-unused \
 -pedantic -std=c++11 -Wall  \
  rinside_sample0.cpp  -L/usr/lib/R/lib -lR \
 -lblas -llapack -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp \
 -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib \
 -L/usr/local/lib/R/site-library/RInside/lib \
 -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib \
 -o rinside_sample0 
edd@max:~/svn/rinside/pkg/inst/examples/standard$ ./rinside_sample0    
Hello, world!        
edd@max:~/svn/rinside/pkg/inst/examples/standard$  

To deploy C++11, you do not need a new compiler or project.

Just add a single option to your compiler flags -- it is that easy.

And just to make it more plain, we can also add these three lines to rinside_sample0.cpp to make it a C++11 program:

auto magic = 42;                    // C++11 
R["magic"] = magic;                    
R.parseEvalQ("print(magic)");     

and (with the required -std=c++11 flag) it too builds and runs. Stricly no change required at the Rcpp or RInside end to deploy them with a C++11-capable compiler.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Sorry I am a bit lost. Do you suggest that I do not need to recompile RInside and Rcpp? – beuhbbb Oct 16 '13 at 13:59
  • That is what my example just demonstrated. – Dirk Eddelbuettel Oct 16 '13 at 14:01
  • Rcpp 0.10.4 is an older release. Try the newest. Try everything _without any modifications_ to demonstrate that your system is not hosed. Then sprinkle in modifications. The rcpp-devel list may be able to help you as there are quite a few OS X users. – Dirk Eddelbuettel Oct 16 '13 at 14:15
  • There may well be an interaction with a new `clang++` -- we release Rcpp / Rinside for CRAN, and it passes all (sensible) systems and OSs that CRAN tests on (with the noted exception of Solaris which nobody uses, no nobody can help us with patches). That includes OS X. You are using it beyond those specs; as I showed that still works on my system and I do not have access to one like yours. – Dirk Eddelbuettel Oct 16 '13 at 14:21
  • Choosing a newer version does not remove me the "Undefined symbols for architecture x86_64". I do not understand why I need to recompile Rcpp and RInside to have compatible version while you not need it. – beuhbbb Oct 16 '13 at 14:22
  • I guess that it is because I use a apple version of clang. – beuhbbb Oct 16 '13 at 14:27
  • I can mix `clang/clang++` and `gcc/g++` just fine on Linux. I just rebuilt the example with `clang++`. – Dirk Eddelbuettel Oct 16 '13 at 14:28