[Rd] forcing the use of the c++ linker with c++ and fortran sources

Steve Guerrero guerrero.steve at gene.com
Sat Jan 10 02:31:18 CET 2009

Sorry I was a little stingy with details.

I'm building a very simple package on linux with R 2.7.1
no configure script or homemade makefiles.
my src directory has two files:

I run "R CMD install myproject " which runs fine, no errors, and 
produces a shared library

gfortran -fdefault-real-8 -ffixed-form -fpic -g -O2 -c  GLMnet.f90 -o 
g++ (all the right flags) -fpic  -g -O2 -c foo.cpp -o foo.o

and the link step:

gfortran -shared -L/usr/local/lib64 -o myproject.so baro foo.o 
-L/apps/R/R-2.7.1/x86_64-linux-2.6/lib64/R/lib -lR

The build goes fine, the problem is when I load the library I get this 
Error in dyn.load(file, DLLpath = DLLpath, ...) :
   unable to load shared library myproject.so
   undefined symbol: __gxx_personality_v0
Error: package/namespace load failed for 'myproject'

If I manually run the link step with g++ instead of gfortran, the 
library loads fine, everything works.

I need to tell the R build process to not link with gfortran but to link 
with g++ in my mixed c++ and fortran scenario.  A little digging into 
the R SHLIB build script suggests that I probably can't override it 
without cooking up my Makefiles

Here's an excerpt from the R SHLIB script:

if ${with_f9x}; then
     makeargs="SHLIB_LDFLAGS='\$(SHLIB_FCLDFLAGS)' ${makeargs}"
     makeargs="SHLIB_LD='\$(SHLIB_FCLD)' ${makeargs}"
   if ${with_cxx}; then
     makeargs="SHLIB_LDFLAGS='\$(SHLIB_CXXLDFLAGS)' ${makeargs}"
     makeargs="SHLIB_LD='\$(SHLIB_CXXLD)' ${makeargs}"
   if ${with_f77}; then
     if ${with_objc}; then
       shlib_libadd="\$(OBJC_LIBS) ${shlib_libadd}"
     if test -z "${shlib_libadd}"; then
       makeargs="${makeargs} SHLIB_LIBADD='\$(FLIBS)'"
       makeargs="${makeargs} SHLIB_LIBADD='\$(FLIBS) ${shlib_libadd}'"
     if ${with_objc}; then
       makeargs="${makeargs} SHLIB_LIBADD='\$(OBJC_LIBS)'"

This logic seems to suggest that if there are fortran95 sources, the LD 
command will be SHLIB_FCLD...with no apparent way to override it.

Am I mistaken?


Duncan Murdoch wrote:
> On 09/01/2009 4:04 PM, Steve Guerrero wrote:
>> I'm trying to build a package which has both gfortran and c++ code.
>> In my installation of R, the R CMD INSTALL process compiles each c++ 
>> and gfortran code with the correct compiler, but performs the link 
>> step with "gfortran" and this creates undefined symbols at load time.
> You should offer more details:  what platform are you working on? What 
> version of R? Do you have a simple example that people could try, and 
> see the errors?
> Duncan Murdoch
>> How does one override/force the use of a different command in the link 
>> step?  Is there an appropriate entry in Makevars that will do the trick?
>> Thanks,
>> -sg
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel

More information about the R-devel mailing list