[R] loading compiled C++ code as shared library

Faheem Mitha faheem at email.unc.edu
Wed Jul 24 23:20:48 CEST 2002

Dear People,

Over the last few days I've been writing C++ to compile as a shared
library (previously I have always used C). Not entirely to my surprise, I
am getting errors. Specifically, when I try to load the shared library, I

Error in dyn.load(x, as.logical(local), as.logical(now)) :
	unable to load shared library
  /home/faheem/research/thesis/sim/ms/ms.so: undefined symbol:

The structure of the C++ file is as follows. I'm concatenating the header
file (.hh) and the actual source code file (.cc) here. (I'm not including
any real code here, since I hope this is just a syntax issue). I have

//some initial header stuff

#define INITTIME 100
#define LOWERLIM -1000000.0
#define UPPERLIM  1000000.0

using std::vector;
using std::equal;

// define some structs

struct alpha {...}; ...

//declare functions


//function definitions

{ bar();


//end c++ file

In a separate R file I am trying trying to call foo.

result <- .C("foo",...) ...

I have read the instructions in the "R Extensions manual" and it appears I
need to declare (at least) the function that I am calling from R with
external linkage. So I added extern "C" next to the declaration of the
function foo. This didn't work. Then I wrapped all the declarations in a

extern "C"{.....}

I tried other variations, including putting all the definitions in an
extern "C" block. I still get the same error. Since I don't really
understand what the extern "C" is supposed be doing here, I'm being pretty
brainless about this.

Note of course I'm compiling with a C++ compiler (g++)

	MAKEFLAGS='CXX=\ g++-3.1 PKG_CXXFLAGS=\ -Wall\ -pedantic\ -Werror\
-Wshadow ' R CMD SHLIB ms.cc -o ms.so

Would someone be kind enough to tell me what the correct syntax is? It
might also be helpful if someone could point to R code that uses
compiled C++ code.

Addendum: I looked at the file svm.h in the e1071 package source, and
tried to imitate that, but I still get the same error.

                                        Sincerely, Faheem Mitha.

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