Re: R-beta: linking c++ functions

Kung-Sik Chan (
Tue, 16 Sep 1997 09:36:29 -0700

Date: Tue, 16 Sep 1997 09:36:29 -0700
From: Kung-Sik Chan <>
To: Ross Ihaka <>
Subject: Re: R-beta: linking c++ functions


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       $(OBJS)
        $(LD) $(SHLIBLDFLAGS) -o $(LIBNAME).so $(OBJS)  \
        -lnewmat -lm

        @rm -f *.o *.so

        @rm -f Makefile *.o *.so       

r-help mailing list -- Read
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: