Re: [R] Include C++ DLL, error in ...: C symbol name not in load table

From: Duncan Murdoch <murdoch.duncan_at_gmail.com>
Date: Wed, 20 Apr 2011 16:42:39 -0400

On 20/04/2011 4:38 PM, Dirk Eddelbuettel wrote:
>
> On 20 April 2011 at 16:24, Duncan Murdoch wrote:
> | On 20/04/2011 4:06 PM, Sascha Vieweg wrote:
> |> Hello R experts
> |>
> |> I am googling and reading around, however, I can't get it working
> |> (perhaps because I do not understand much C, however, I'll give it
> |> a try). I am trying to include C++ code into an R routine, where
> |> the C++ code looks:
> |>
> |> #include<iostream>
> |> using namespace std;
> |> void foo (double* x, double* y, double* out)
> |> {
> |> out[0] = x[0] + y[0];
> |> }
> |>
> |> Back in R, the command
> |>
> |> R CMD SHLIB --preclean -o xplusy
> |>
> |> works fine resulting in two new files, xplusy.o and xplusy.so. The
> |> wrapper in R is:
> |>
> |> dyn.load("xplusy.so")
> |> xplusy<- function(x, y){
> |> .C("foo", as.double(x), as.double(y), out=double(1))$out
> |> }
> |> xplusy(1, 2)
> |> dyn.unload("xplusy.so")
> |>
> |> Now, dyn.load() works and xplusy also shows up in getLoadedDLLs().
> |> However, when invoking the function, xplusy(1, 2), R complains:
> |>
> |> Error in .C("foo", as.double(x), as.double(y), out = double(1)): C
> |> symbol name "foo" not in load table
> |>
> |> I found some hints concerning Fortran code producing this error
> |> message, but no help concerning C code.
> |
> | You have C++ code, not C code. C++ normally mangles names of exports.
> |
> | To get this to work, you should surround your declarations with
> |
> | extern "C" {
> | }
> |
> | Another possibility is to use the Rcpp package; it writes the interface
> | code for you.
>
> I believe Duncan refers to the 'inline' package, rathern than 'Rcpp' (which
> itself uses 'inline').

Oops, Dirk is right.

Duncan Murdoch



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 20 Apr 2011 - 20:46:00 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 20 Apr 2011 - 21:40:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive