[R-SIG-Mac] Compiling a portable binary that includes Fortran code

Steve Lianoglou mailinglist.honeypot at gmail.com
Tue Sep 20 04:41:57 CEST 2011


Hi,

On Mon, Sep 19, 2011 at 8:23 PM, Michael Braun <braunm at mit.edu> wrote:
> 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.

Are your colleagues also running OS X?

I think you should be able to make your life a lot easier by simply
running an `R CMD BUILD yourpkg` which (if I'm not mistaken) will
compile your package into a form your end-users/colleagues will be
able to just `R CMD INSTALL` without requiring any compilers -- just
like they can download binaries of R packages from CRAN w/o needing to
compile it themselves.

Does that help?

-steve


  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
>
> _______________________________________________
> R-SIG-Mac mailing list
> R-SIG-Mac at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-mac
>
>



-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-SIG-Mac mailing list