[Rd] Linking to lapack
shotwelm at musc.edu
Fri Oct 22 15:13:15 CEST 2010
On Fri, 2010-10-22 at 05:30 -0400, Nick Sabbe wrote:
> Hello all.
> I'm developing a package for R holding a Gibbs sampler, which tends to have
> better performance when written in C than in R.
> During each iteration in the Gibbs sampler, I need the inverse of a
> symmetric matrix.
> For this, I wish to use lapack, as is concisely suggested in "Writing R
> extensions", since this will have better performance than I could ever write
> After some twiddling I have got my code to compile by including
> "R_ext/Lapack.h" and using "F77_CALL(dpotrf)", but unfortunately, I don't
> get this to link properly.
> I get this message: " testc.o:testc.c:(.text+0x255): undefined reference to
> `dpotrf_'" which seems logical to me as far as my understanding of C
> reaches, but I don't know how to resolve it. I'm quite sure I need some
> extra parameters in my makefile, but as I come from a world where all these
> complexities are happily abstracted away for me by an IDE, I have no actual
> clue on how to surmount this.
Yes. You need to ensure that your program is linked with a Lapack
library where the symbol dpotrf_ is defined. In an R package, using a
'Makevars' file in your 'src' directory is used to specify additional
linking. See "Writing R Extensions" section 1.2.1 Using ‘Makevars’. If
you are compiling your code with a custom Makefile, then you will need
to add an argument to the linker/compiler specifying a Lapack library to
be linked. For example, here is a command that dynamically links the R
Lapack and Blas libraries to a pre-compiled 'test.o' on my system:
gcc -std=gnu99 -shared -L/usr/local/lib -o test.so test.o
-L/usr/local/lib/R/lib -lRlapack -L/usr/local/lib/R/lib -lRblas
> However: when I'm done with all my code, I wish to build a package for
> publication on CRAN, so I want to be sure that not only I can build it on my
> system, but it will also work well when distributed to other computers (if I
> understand the package process well, source files are compiled and linked
> during installation of the package), so I would also like to know how to do
It might be a good idea to build your package simultaneously with your
code, and use a Makevars file.
> It should not be relevant, but either way: I'm doing all this on a Windows 7
> machine, though the package will probably be used on Linux-based servers
> Finally: I have found no comprehensive list of the functions available to an
> R package developer, nor, strangely, questions about that. Does such a thing
> exist, or are we up to hoping we find what we are looking for in the header
> files? If it does not exist already, I would surely be willing to work on
Three of the R manuals (R-exts, R-ints, R-admin;
http://cran.r-project.org/doc/manuals/) partially describe some aspect
of the R API, where "Writing R Extensions" is the most comprehensive.
Function descriptions are generally not biolerplate. For good or bad, a
look into the C headers is often necessary to use an R API function.
> Thanks for any input.
> Nick Sabbe
> ping: nick.sabbe at ugent.be
> link: http://biomath.ugent.be
> wink: A1.056, Coupure Links 653, 9000 Gent
> ring: 09/264.59.36
> -- Do Not Disapprove
> R-devel at r-project.org mailing list
Matthew S. Shotwell
Division of Biostatistics and Epidemiology
Medical University of South Carolina
More information about the R-devel