[R] Compiling C++ package source: linking problem?

Alet Roux A.Roux at hull.ac.uk
Thu Jun 17 01:09:52 CEST 2004


Dear Prof Ripley

libR.a seems to be created fine, as far as this layman can tell. Executing 

make libR.a

in C:\R\rw1090\src\gnuwin32 produces the single line

dlltools -k --as as  --dllname R.dll --def R.exp --output-lib libR.a

and the resulting file libR.a is 1583 Kb big. I just tried copying my c++ file
into C:\R\rw1090\src\gnuwin32, and executing R CMD SHLIB as below. It seems to
work (so libR.a must be fine), but is not an ideal solution, for obvious
reasons.

Now, compiling my second example below (it does in fact have an extern
"C"-wrapper), I typed

R CMD SHLIB montecarlo.cpp

in the appropriate directory, and obtained (slight alterations in order to make
the text fit).

making montecarlo.d from montecarlo.cpp
g++ --shared -s -o montecarlo.dll montecarlo.def montecarlo.a
    -LC:/R/rw1090/src/gnuwin32 -lg2c -lR
montecarlo.a(montecarlo.o.b)(.text+0x28):montecarlo.cpp: undefined reference 
    to 'Rf_allocVector(unsigned, int)'
montecarlo.a(montecarlo.o.b)(.text+0x33):montecarlo.cpp: undefined reference 
    to 'Rf_protect(SEXPREC*)'
montecarlo.a(montecarlo.o.b)(.text+0x3f):montecarlo.cpp: undefined reference 
    to 'Rf_unprotect(int)'
make: ** [montecarlo.dll] Error 1

To me, it seems that libR.a is simply ignored (I had the same impression with
Rdll.lib under MSVC++ 6.0; the linker found and read the file, but gave similar
errors as above).

With regards to the versions of software I'm using:

Rtools package -- downloaded two days ago
gcc -- 3.2.3 (mingw special 20030504-1)
perl -- 5.6.1 from activestate
no cygwin

Thank you very much in advance.

Alet

Quoting Prof Brian Ripley <ripley at stats.ox.ac.uk>:

> On Wed, 16 Jun 2004, Alet Roux wrote:
> 
> > Dear All
> > 
> > I'm currently developing a package for R (1.9.0) on Win32, with C++ source
> code.
> > Having followed the instructions in readme.packages, my code compiles fine
> with
> > R CMD SHLIB (as well as R CMD check) ... until I start using the internal
> R
> > functions. 
> > 
> > (Interesting: Rprintf seems to be the exception.) For instance, the
> following
> > code compiles fine:
> > 
> > #include <R.h>
> > #include <Rdefines.h>
> > ...
> > SEXP whatever (SEXP model)
> > {
> > 	Rprintf ("Hello, here I am!\n");
> >         return model;
> > }
> > 
> > However, the following doesn't compile at all:
> > 
> > #include <R.h>
> > #include <Rdefines.h>
> > ...
> > SEXP whatever (SEXP model)
> > {
> > 	SEXP anotherModel;
> > 	PROTECT(anotherModel = NEW_NUMERIC(4));
> > 	UNPROTECT(1);
> >         return anotherModel;
> > }
> > 
> > An example of compiler feedback from R CMD SHLIB (I have mingw 3.1.0):
> > ...: undefined reference to 'Rf_allocVector(unsigned,int)'
> > ...: undefined reference to 'Rf_protect(SEXPREC*)'
> > ...: undefined reference to 'Rf_unprotect(int)'
> > 
> > Can anybody tell me what the matter is? Did I miss something?
> 
> Are you linking against the R.dll via its import library libR.a?  Did the 
> latter get made correctly?  That is where those entry points are.
> 
> More complete compiler output would have helped here.
> 
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
> 
> 


-------------------------------------
Alet Roux
Department of Mathematics
University of Hull
Kingston upon Hull
HU6 7RX
United Kingdom
URL: http://www.hull.ac.uk/php/mapar/
Tel: +44 (1482) 466463
Fax: +44 (1482) 466218




More information about the R-help mailing list