[R] C++ code from R

Rainer M Krug r.m.krug at gmail.com
Wed Nov 19 14:47:52 CET 2008


On Wed, Nov 19, 2008 at 2:57 PM, Duncan Temple Lang
<duncan at wald.ucdavis.edu> wrote:
>
>
> Rainer M Krug wrote:
>>
>> Hi
>>
>> I managed to compile (and use) C code from R, but now I have to use C++
>> code.
>> After reading "Writing R extensions", I came up with the following
>> code, which compiles fine
>>
>> -------------------
>> // emdL1_R.cc:
>>
>> #include <time.h>
>> #include <stdio.h>
>> #include "emdL1.h"
>>
>> extern "C"
>> {
>>  void emd_L1(
>>              double *H1, int *n1,
>>              double *H2, int *n2,
>>              int *Dims,
>>              double *e
>>              )
>>  {
>>    EmdL1 em;                   // EMD_L1 class
>>
>>    e[0] = em.EmdDist( H1,H2, n1[0],n2[0], Dims[0]);                    //
>> 2D EMD_L1
>>
>>    printf("For 2D histograms,  EmdL1(h1,h2)=%lf\n", e[0]);
>>  }
>> }// extern "C"
>> -------------------
>> -------------------
>> Compilation started at Wed Nov 19 14:30:42
>>
>> R CMD SHLIB emdL1.cpp emdL1_R.cc
>> g++ -I/usr/share/R/include      -fpic  -g -O2 -c emdL1.cpp -o emdL1.o
>> g++ -I/usr/share/R/include      -fpic  -g -O2 -c emdL1_R.cc -o emdL1_R.o
>> g++ -shared  -o emdL1.so emdL1.o emdL1_R.o   -L/usr/lib/R/lib -lR
>>
>> Compilation finished at Wed Nov 19 14:30:44
>>
>> Compilation started at Wed Nov 19 14:37:12
>>
>> R CMD SHLIB emdL1_R.cc
>> g++ -shared  -o emdL1_R.so emdL1_R.o   -L/usr/lib/R/lib -lR
>
>
>
> You should compile all the files into one single .so
> So use the first call to R CMD SHLIB, i.e. (with the order
> of the files switched)
>
>  R CMD SHLIB emdL1_R.cc emdL1_R.cc
>
> and then in R
>
>  dyn.load("emdL1_R.so")
>
> should work.
>
> The problem was that in the second SHLIB call,
> you compiled and linked emdL1_R.cc into emdL1_R.so but didn't
> include the emdL1.cc file which contains the classes and routines you
> refer to in emdL1_R.cc

OK - it is working now - thanks a lot.



>
>  D.
>
>
>
>
>>
>> Compilation finished at Wed Nov 19 14:37:12
>> -------------------
>>
>>  but when I want to load the resulting .so file (emdL1_R.so), I get
>> the following error message:
>>
>> -------------------
>>>
>>> dyn.load("emdL1_R.so")
>>
>> Error in dyn.load("emdL1_R.so") :
>>  unable to load shared library
>> '/home/rkrug/Documents/Projects/EMD/EmdL1_vs/emdL1_R.so':
>>  /home/rkrug/Documents/Projects/EMD/EmdL1_vs/emdL1_R.so: undefined
>> symbol: _ZN5EmdL1D1Ev
>> -------------------
>>
>>
>> The two other files (emdL1.h and emdL1.cpp) are in the following zip file:
>>
>> http://vision.ucla.edu/~hbling/code/EmdL1_v2.zip
>>
>>
>> Any suggestions are welcome,
>>
>> Rainer
>>
>>
>>
>>
>



-- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation
Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Faculty of Science
Natural Sciences Building
Private Bag X1
University of Stellenbosch
Matieland 7602
South Africa



More information about the R-help mailing list