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]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel