[R-SIG-Mac] Compiling a portable binary that includes Fortran code
Michael Braun
braunm at mit.edu
Tue Sep 20 02:23:15 CEST 2011
I am creating an R package that includes some C++ code that I have written, and some "legacy" Fortran code from another source. I am able to build and run this package on my own machine (OSX Lion 10.7.1) with no problem. However, I would like some colleagues to test the package before I post it on CRAN. Unfortunately, what I have created does not appear to be portable, and I would appreciate some guidance.
Ideally, I would not expect my users to have the Apple developer tools, or an R-friendly version of gfortran installed. So I thought I would compile a binary myself, and link to the gfortran libraries statically. My Makevars file has exactly two lines. One is a PKG_CPPFLAGS definition, to point to headers. The other is PKG_LIBS=/usr/local/lib/libgfortran.a . I then build the binary using R CMD INSTALL --binary. The compiler then does it's thing, and when it gets to the link step, I see:
g++-4.2 -arch x86_64 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -o mypkg.so mypkg.o /usr/local/lib/libgfortran.a -lgfortran -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
Note that there is a -lgfortran that I did not include myself.
Of course, I want to follow the R manuals as closely as possible. In Section 1.7.2 of "Writing R Extensions," there is advice to be aware of any dependencies. So I then ran R CMD otool -L mypkg.so , and got the following:
mypkg.so:
mypkg.so (compatibility version 0.0.0, current version 0.0.0)
/usr/local/lib/libgfortran.2.dylib (compatibility version 3.0.0, current version 3.0.0)
/Library/Frameworks/R.framework/Versions/2.13/Resources/lib/libR.dylib (compatibility version 2.13.0, current version 2.13.1)
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 635.0.0)
/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.0.0)
So it looks like there are still some dependencies that, at least according to Section 1.7.2, I don't want. But I do not know how to get rid of them. And when my colleague loads the file, of course there is an error, because R cannot find the correct libgfortran dynamic library.
I also tried something else. I know that R comes packaged with libgfortran.2.dylib in the $R_HOME/lib directory. So what I thought I would do is add -L$(R_HOME)/lib to PKG_LIBS *instead* of doing the static linking thing. The idea is that all users would have that version of gfortran in the same place. But when I do that, the linker still seems to want to link to the libgfortran.3.dylib that is in /usr/local/lib, and not the libgfortran.2.dylib that is in $R_HOME/lib. This happens even if I change the dynamic link of libgfortran.dylib to .2 instead of .3. When my colleague tries to load mypkg.so, he gets an error that includes: Library not loaded: /usr/local/lib/libgfortran.3.dylib
So really, all I want to do is create a binary someone can just load, without needing to preinstall lots of other stuff. It doesn't matter to me if it involves static linking to runtime libraries, or telling the loader where to find the dynamic libraries.
Thanks in advance for your help with this. It is much appreciated.
Michael Braun
MIT Sloan School of Management
braunm at mit.edu
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1844 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-mac/attachments/20110919/4a9ad797/attachment.bin>
More information about the R-SIG-Mac
mailing list