Quantcast

Include C++ DLL, error in ...: C symbol name not in load table

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Include C++ DLL, error in ...: C symbol name not in load table

Sascha Vieweg
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.

Any help tips?

Thanks, *S*

$. uname -a
Darwin * 10.7.3 Darwin Kernel Version 10.7.3[...]

$: c++ --version
i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666)
(dot 3)

platform       i386-apple-darwin9.8.0
arch           i386
os             darwin9.8.0
system         i386, darwin9.8.0
status
major          2
minor          12.2
year           2011
month          02
day            25
svn rev        54585
language       R
version.string R version 2.12.2 (2011-02-25)

--
Sascha Vieweg, [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Duncan Murdoch-2
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.

Duncan Murdoch

>
> Any help tips?
>
> Thanks, *S*
>
> $. uname -a
> Darwin * 10.7.3 Darwin Kernel Version 10.7.3[...]
>
> $: c++ --version
> i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666)
> (dot 3)
>
> platform       i386-apple-darwin9.8.0
> arch           i386
> os             darwin9.8.0
> system         i386, darwin9.8.0
> status
> major          2
> minor          12.2
> year           2011
> month          02
> day            25
> svn rev        54585
> language       R
> version.string R version 2.12.2 (2011-02-25)
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Dirk Eddelbuettel

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').

Dirk

--
Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Duncan Murdoch-2
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Massimiliano
In reply to this post by Sascha Vieweg
Maybe you have to add

#include <stdio.h>
#include <R.h>

to your C++ source code.

Massimiliano Tripoli
Il giorno mer, 20/04/2011 alle 22.06 +0200, Sascha Vieweg ha scritto:
> #include <iostream>
> using namespace std;
> void foo (double* x, double* y, double* out)
> {
>         out[0] = x[0] + y[0];
> }
>
>

______________________________________________
[hidden email] 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.
Loading...