R-beta: linking c++ functions

Kung-Sik Chan kchan at stat.uiowa.edu
Tue Sep 16 18:36:29 CEST 1997


A few days ago, I posted a question on how to link C++ functions
to R. I have now solved the problem, with my C++ function using
the newmat library now successfully called from R. The tricks are:
(1) use g++ to do the linking,
(2) modify the C++ functions, only those that you need to call from R,
so that they are callable by C codes.
The scond step is rather simple. Suppose that we want to call 
void f(int *i, char *c, double *x) 
from R.  
In the file containing f, add the line
extern "C" void f(int *i, char *c, double *x);
before the definition of f. This statement tells the compiler that
the external information sent to the linker should use C calling
conventions and name mangling. I learn this trick from Marshall Cline's
C++ faq
under the item of how to mix C and C++.

The Makefile file in the src directory of my chaos library is as

        g++ -c -fPIC -I/usr/include/newmat -I/usr/include/nric $<

OBJS=   lyapunov.o

chaos.so:       $(OBJS)
        $(LD) $(SHLIBLDFLAGS) -o $(LIBNAME).so $(OBJS)  \
        -lnewmat -lm

        @rm -f *.o *.so

        @rm -f Makefile *.o *.so       

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list