Help on interfacing C++ with R

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

Help on interfacing C++ with R

Sun.Bing
Hi, I am trying to set up a C++ library for my R code. I followed the
R-extension manual but found out that the example of "X.cpp, X_main.cpp"
is somewhat too simple. Here is my code:

 //lib4R.h testing for interfacing C++ with R -- using C++ library in R
#include <iostream>
using namespace std;

class lib4R {
public:
        lib4R();
        ~lib4R();

        int testIntDivision(int, int);
        double testDoubleMultiplication(double, double);

        int getID();
        void setID(int);
private:
        int ID;
};

// lib4R.cpp : Defines the entry points for the library.
#include "lib4R.h"

lib4R::lib4R() {
        cout << "Constructor lib4R()" << endl;
}

lib4R::~lib4R() {
        cout << "Destructor ~lib4R()" << endl;
}

extern "C" {

int lib4R::testIntDivision(int a, int b) {
      return a/b;
}

double lib4R::testDoubleMultiplication(double a, double b) {
        return a*b;
}

int lib4R::getID() {
        return this->ID;
}

void lib4R::setID(int ID) {
        this->ID = ID;
}

int main(int argc, char* argv[])
{
        cout << "Entering main()" << endl;
        lib4R lib1;

        cout << "testIntDivision(4,2) = " << lib1.testIntDivision(4,2) <<
endl;

        return 0;
}

} // extern C

I am working on Windows. I use these to compile the dll with Visual C++:

cl /MT /c lib4R.cpp
link /dll /out:lib4R.dll /export:testIntDivision /export:main lib4R.obj

So when I start rterm.exe:

>dyn.load("../lib4R.dll");
>.C("main");
Entering main()
Constructor lib4R()
testIntDivision(4,2) = 2
Destructor ~lib4$()
list()

The output is correct, but if I
>.C("testIntDivision", 4, 2);

it generated the dialog box claiming "R has encountered a problem and
needs to be closed.......Please tell Microsoft about this problem....",
sigh.

So what's wrong with my code, how do I use individual methods/functions of
C++ in R?

Thanks in advance for any help you can offer!

Bing

        [[alternative HTML version deleted]]

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

Re: Help on interfacing C++ with R

Roger Bivand
On Mon, 13 Mar 2006 [hidden email] wrote:

> Hi, I am trying to set up a C++ library for my R code. I followed the
> R-extension manual but found out that the example of "X.cpp, X_main.cpp"
> is somewhat too simple. Here is my code:

Simpler is easier, all this std stuff gets in the way. The problem is that
R is passing pointers (probably) to doubles, not simple integers. As far
as I can see, you need to change the

.C("testIntDivision", 4, 2);

to make sure your numbers are integers (as.integer()) is OK), and the
function itself (provided that it is visible, and I've no idea if this is
the case, to pointer arguments:

int testIntDivision(int&, int&); // untried

then pick out the integer values at those addresses. It'll be one or both
of these problems (I am not fluent in C++, but the same principles apply
to C).

I would be concerned about exporting any competing main to load
dynamically into R too, could you compile the code in two files, one the
code to load into R, the other with main calling your code, and *only*
load what needs to be inside R?

>
>  //lib4R.h testing for interfacing C++ with R -- using C++ library in R
> #include <iostream>
> using namespace std;
>
> class lib4R {
> public:
>         lib4R();
>         ~lib4R();
>
>         int testIntDivision(int, int);
>         double testDoubleMultiplication(double, double);
>
>         int getID();
>         void setID(int);
> private:
>         int ID;
> };
>
> // lib4R.cpp : Defines the entry points for the library.
> #include "lib4R.h"
>
> lib4R::lib4R() {
>         cout << "Constructor lib4R()" << endl;
> }
>
> lib4R::~lib4R() {
>         cout << "Destructor ~lib4R()" << endl;
> }
>
> extern "C" {
>
> int lib4R::testIntDivision(int a, int b) {
>       return a/b;
> }
>
> double lib4R::testDoubleMultiplication(double a, double b) {
>         return a*b;
> }
>
> int lib4R::getID() {
>         return this->ID;
> }
>
> void lib4R::setID(int ID) {
>         this->ID = ID;
> }
>
> int main(int argc, char* argv[])
> {
>         cout << "Entering main()" << endl;
>         lib4R lib1;
>
>         cout << "testIntDivision(4,2) = " << lib1.testIntDivision(4,2) <<
> endl;
>
>         return 0;
> }
>
> } // extern C
>
> I am working on Windows. I use these to compile the dll with Visual C++:
>
> cl /MT /c lib4R.cpp
> link /dll /out:lib4R.dll /export:testIntDivision /export:main lib4R.obj
>
> So when I start rterm.exe:
>
> >dyn.load("../lib4R.dll");
> >.C("main");
> Entering main()
> Constructor lib4R()
> testIntDivision(4,2) = 2
> Destructor ~lib4$()
> list()
>
> The output is correct, but if I
> >.C("testIntDivision", 4, 2);
>
> it generated the dialog box claiming "R has encountered a problem and
> needs to be closed.......Please tell Microsoft about this problem....",
> sigh.
>
> So what's wrong with my code, how do I use individual methods/functions of
> C++ in R?
>
> Thanks in advance for any help you can offer!
>
> Bing
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Roger Bivand
Department of Economics
NHH Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway