I solved this issue, which turned out to be related to how I was providing the argument -stdlib=libstdc++
to R CMD SHLIB
. R CMD SHLIB
invokes clang++
twice, first as a compilation phase to build an object file (simple.o
in my case) and then to link that file into a shared object (simple.so
in my case). R CMD SHLIB
was only passing the -stdlib=libstdc++
argument to the second invocation, but we need it to also provide the argument to the first invocation of clang++
. We can do this by adding -stdlib=libstdc++
to PKG_CXXFLAGS
:
$ PKG_CXXFLAGS="-I/Library/gurobi562/mac64/include -stdlib=libstdc++" R CMD SHLIB \
simple.cpp -L/Library/gurobi562/mac64/lib -lgurobi_c++ -lgurobi56 \
-stdlib=libstdc++ -lpthread -lm
clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG \
-I/usr/local/include -I/Library/gurobi562/mac64/include -stdlib=libstdc++ \
-fPIC -mtune=core2 -g -O2 -c simple.cpp -o simple.o
clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup \
-single_module -multiply_defined suppress -L/usr/local/lib -L/usr/local/lib \
-o simple.so simple.o -L/Library/gurobi562/mac64/lib -lgurobi_c++ -lgurobi56 \
-stdlib=libstdc++ -lpthread -lm -F/Library/Frameworks/R.framework/.. \
-framework R -Wl,-framework -Wl,CoreFoundation
Now, dyn.load("simple.so")
works without error from R (though as mentioned by @MartinMorgan and @JanvanderLaan, I'll need to expose my functions using extern "C"
or an alternative to actually be able to call them from R).