Interfacing R and C++

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Interfacing R and C++

Sean Davis
I have a single c++ file that contains a class and a "wrapper" function that
has C-like syntax for interacting with the Class.  Basically, this wrapper
function just makes an instance of the class and then organizes the data for
return to R.  

#include <math.h>

void myFunc(double *data, int *n, double *prm, double* intervals, int* max,
double *plot) {
...
}

myClass::myclass(...) {
...
}

My question is simple:  what goes inside the extern "C" {} block.  Should it
include only the #includes, those and the "wrapper" function, or the entire
.cc file?  The answer wasn't clear to me from the Writing R extensions
manual (probably due to my c++ ignorance--using someone else's code).

> sessionInfo()
R version 2.2.0, 2005-08-11, powerpc-apple-darwin7.9.0

attached base packages:
[1] "methods"   "stats"     "graphics"  "grDevices" "utils"     "datasets"
[7] "base"

I'm trying to use the .C interface and getting segmentation faults--probably
a bug in the code, but just wanted to make sure that it wasn't a simple
issue with the extern block.

Thanks,
Sean

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Interfacing R and C++

Brian Ripley
The wrapper function must be in extern "C" {} since you want to give it a
C name (and not a mangled C++ one).  These days, the headers probably do
not need to be, but in theory if they are C headers they should be in any
C++ code.

I think R-exts is quite clear on this:

   To use with @R{}, the only thing we have to do is writing a wrapper
   function and ensuring that the function is enclosed in
                              ^^^^^^^^^^^^^^^^^^^^^^^^



On Mon, 9 Jan 2006, Sean Davis wrote:

> I have a single c++ file that contains a class and a "wrapper" function that
> has C-like syntax for interacting with the Class.  Basically, this wrapper
> function just makes an instance of the class and then organizes the data for
> return to R.
>
> #include <math.h>
>
> void myFunc(double *data, int *n, double *prm, double* intervals, int* max,
> double *plot) {
> ...
> }
>
> myClass::myclass(...) {
> ...
> }
>
> My question is simple:  what goes inside the extern "C" {} block.  Should it
> include only the #includes, those and the "wrapper" function, or the entire
> .cc file?  The answer wasn't clear to me from the Writing R extensions
> manual (probably due to my c++ ignorance--using someone else's code).
>
>> sessionInfo()
> R version 2.2.0, 2005-08-11, powerpc-apple-darwin7.9.0
>
> attached base packages:
> [1] "methods"   "stats"     "graphics"  "grDevices" "utils"     "datasets"
> [7] "base"
>
> I'm trying to use the .C interface and getting segmentation faults--probably
> a bug in the code, but just wanted to make sure that it wasn't a simple
> issue with the extern block.
>
> Thanks,
> Sean
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Interfacing R and C++

Sean Davis



On 1/9/06 3:20 PM, "Prof Brian Ripley" <[hidden email]> wrote:

> The wrapper function must be in extern "C" {} since you want to give it a
> C name (and not a mangled C++ one).  These days, the headers probably do
> not need to be, but in theory if they are C headers they should be in any
> C++ code.
>
> I think R-exts is quite clear on this:
>
>    To use with @R{}, the only thing we have to do is writing a wrapper
>    function and ensuring that the function is enclosed in
>                               ^^^^^^^^^^^^^^^^^^^^^^^^

Yes, the documentation is clear, provided one knows what name-mangling is
and why it is important, which I now understand better.

Thanks,
Sean

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel