2

I recently moved to C++11 and Xcode 5 with command line tools on OS X 10.9.1, installed boost 1.55 using Homebrew and changed the compiler from g++ to clang++ (as mentioned in this post: Error when with Xcode 5.0 and Rcpp). The compiler change fixed Rcpp when running in R. But I am having issues with compiling the RInside examples (which used to work just fine).

I re-downloaded RInside and unzipped the tar, went to the examples/standard directory and did a make clean and make all. It looks like any STL is not being linked to from this directory. How do I make RInside link to the appropriate STL in the Makefile?

I am using STL from Netbeans in a seperate project and building using clang++ works there. However, when I try an RInside project on Netbeans I get the same issue: Undefined symbols for architecture x86_64. Do I need to download a 64 bit version of STL or of RInside somewhere ?

Am I still in need of updating/altering a config somewhere, did I download a wrong package or is it a setting in the Makefile for the examples that needs to change?

Here is the first call from make all:

clang++ -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include -I/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RInside/include -mtune=native -g -O2 -Wall -pedantic -Wconversion -Wall -I/usr/local/include   rinside_callbacks0.cpp  -F/Library/Frameworks/R.framework/.. -framework R  -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/Library/Frameworks/R.framework/Resources/lib -lRlapack /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/lib/libRcpp.a /Library/Frameworks/R.framework/Versions/3.0/Resources/library/RInside/lib/libRInside.a -o rinside_callbacks0

The beginning of the output:

Undefined symbols for architecture x86_64:
  "std::string::find_last_of(char, unsigned long) const", referenced from:
      demangler_one(char const*) in libRcpp.a(api.o)
  "std::string::find(char const*, unsigned long) const", referenced from:
      short_file_name(char const*) in libRcpp.a(api.o)
  "std::string::size() const", referenced from:
      std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::operator+<char, std::char_traits<char>, std::allocator<char> >(char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) in libRcpp.a(api.o)
      MemBuf::add(std::string const&) in libRInside.a(MemBuf.o)
  "std::string::c_str() const", referenced from:
      RInside::parseEval(std::string const&, SEXPREC*&) in libRInside.a(RInside.o)
      SEXPREC* Rcpp::pairlist<Rcpp::Function, Rcpp::traits::named_object<SEXPREC*>, Rcpp::traits::named_object<SEXPREC*> >(Rcpp::Function const&, Rcpp::traits::named_object<SEXPREC*> const&, Rcpp::traits::named_object<SEXPREC*> const&) in libRInside.a(RInside.o)
      short_file_name(char const*) in libRcpp.a(api.o)
      string_to_try_error(std::string const&) in libRcpp.a(api.o)
      Rcpp::RObject::AttributeProxy::set(SEXPREC*) const in libRcpp.a(api.o)
      demangle(std::string const&) in libRcpp.a(api.o)
      Rcpp::RObject::AttributeProxy::get() const in libRcpp.a(api.o)

And at the end of the error messages:

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Community
  • 1
  • 1
crogg01
  • 2,446
  • 15
  • 35
  • I just noticed there is an Rcpp11 project. Does this mean that Rcpp and RInside won't play nice with C++11 ? I get the same error when I set the `--std=c++98` in Netbeans though. I will try downgrading my Xcode. – crogg01 Jan 18 '14 at 18:20
  • 1
    No. Rcpp could not / cannot until R 3.1.0 is release use C+11 as CRAN would not accept it. So our use of it in Rcpp is entirely options -- but see for example the [couple of posts on Rcpp and C++11 at the Rcpp Gallery](http://gallery.rcpp.org/tags/c++11/). We even have an attribute to turn C++11 on by demand. Just don't upload C++11 code to CRAN yet. – Dirk Eddelbuettel Jan 18 '14 at 20:29

2 Answers2

1

I suggest you study / follow the discussions on the r-sig-mac list as well as here on SO (search for, say, [rcpp] mavericks).

I suspect that all you need is to expand your CXX and CXXFLAGS variables to say

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

plus whatever other options such as -O3 -Wall -pedantic.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • I already tried those and it didn't work both through ~/.R/Makevars and environment variables. I downgraded to XCode 4.6.3 and I am installing g++ separately. We will see what comes out of it. – crogg01 Jan 18 '14 at 20:36
  • Hm. RInside should not need much more than Rcpp so that is a little puzzling. Maybe try the rcpp-devel list too. – Dirk Eddelbuettel Jan 18 '14 at 20:39
  • The `make` call you quote does not seem to link a C++ library. Maybe you need an additional LDLIBS to set it? I think this should work with XCode, but as I said: not a Mac user myself. – Dirk Eddelbuettel Jan 18 '14 at 20:41
  • the clang++ changes make Rcpp work in R (e.g. `evalCpp('2LL')`) but the RInside examples refused to link. It could be due to some conflict with Xcode 5.0.2/C++11/RInside. – crogg01 Jan 18 '14 at 20:42
  • That makes a lot of sense, I will try it manually and post an update – crogg01 Jan 18 '14 at 20:43
  • I got the same problem with this call: `clang++ -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include -I/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RInside/include -stdlib=libc++ -std=c++11 -mtune=native -g -O3 -Wall -pedantic -Wconversion -Wall -I/usr/local/include rinside_callbacks0.cpp IGNORING THIS BECAUSE TOO LONG FOR COMMENT -o rinside_callbacks0` , if I change to `-std=c++98` I get the same issue (though now I downgraded to Xcode 4.6.3). – crogg01 Jan 18 '14 at 20:51
  • Try a hello_world.cpp (no R or Rcpp or RInside) using `std::string` and `std::cout`, say, and write down what you need there. Adjust the makefile variable to provide that, and play with order of libraries linked. There is no magic here -- you need to provide the C++ libraries for this to work. – Dirk Eddelbuettel Jan 18 '14 at 20:58
1

I got the example to work from my Netbeans after downgrading to Xcode 4.6.3 on OS X 10.9.1 and using clang++ and C++11 (no other changes to the settings in Netbeans). It also worked on g++ (or clang++) using C++98. It looks like the default library in Xcode 5 was libc++ (LLVM C++ standard library with C++11 support) but what I needed for this to link was libstdc++ (GNU C++ standard library - default library for earlier Xcode versions). After changing that in the ~/.R/Makevars file CXXFLAGS="-stdlib=libstdc++ -std=c++11 -mtune=native -g -O3 -Wall -pedantic -Wconversion" along with the clang++ change I got the examples to build. I confirmed the behavior by re-upgrading to Xcode 5 and getting the same error in Netbeans but not on the samples (which now built because of the libstdc++ flag). When I added -stdlib=libstdc++ as an additional flag in Netbeans it built again.

Just as an fyi in case someone else hits a problem building the examples. In Netbeans the compile and links steps are:

"/Applications/Xcode.app/Contents/Developer/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/Applications/Xcode.app/Contents/Developer/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/callingrproject
mkdir -p build/Debug/GNU-MacOSX
rm -f "build/Debug/GNU-MacOSX/main.o.d"
clang++ -stdlib=libstdc++   -c -g -I. -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/library/Rcpp/include -I/Library/Frameworks/R.framework/Resources/library/RInside/include -I/usr/local/Cellar -std=c++98 -MMD -MP -MF "build/Debug/GNU-MacOSX/main.o.d" -o build/Debug/GNU-MacOSX/main.o main.cpp
mkdir -p dist/Debug/GNU-MacOSX
clang++ -o dist/Debug/GNU-MacOSX/callingrproject build/Debug/GNU-MacOSX/main.o -L/Library/Frameworks/R.framework/Resources/lib -L/Library/Frameworks/R.framework/Resources/library/Rcpp/lib -L/Library/Frameworks/R.framework/Resources/library/RInside/lib -L/Library/Frameworks/R.framework/Libraries -L/Library/Frameworks/R.framework/Resources/lib -L/Library/Frameworks/R.framework/Resources/library -L/Library/Frameworks/R.framework/Resources/modules -lRcpp -lRInside -lRlapack -lRblas -F/Library/Frameworks/R.framework/.. -framework R -stdlib=libstdc++

The example build was:

clang++ -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include -I/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RInside/include -stdlib=libstdc++ -std=c++11 -mtune=native -g -O3 -Wall -pedantic -Wconversion -Wall -I/usr/local/include   rinside_callbacks0.cpp  -F/Library/Frameworks/R.framework/.. -framework R  -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/Library/Frameworks/R.framework/Resources/lib -lRlapack /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/lib/libRcpp.a /Library/Frameworks/R.framework/Versions/3.0/Resources/library/RInside/lib/libRInside.a -o rinside_callbacks0
crogg01
  • 2,446
  • 15
  • 35