Hi, all I didn't get any response from swig for my question. see if I can get some help here Thanks ---------- Forwarded message ---------- From: charlie <charlie.xia.fdu@gmail.com> Date: Tue, Nov 4, 2008 at 1:55 PM Subject: SWIG with R and C++ STL To: swig-user@lists.sourceforge.net Hi all, I am new to SWIG. I encountered some problem when I try to SWIG to R some C++ modules. Here is the details. I got "myvector.i" and "myvector.h" as my two input files, the contends are: ---myvector.i----- %module myvector %{ #include "myvector.h" %} %include "std_vector.i" namespace std { %template(IntVector) vector<int>; %template(DoubleVector) vector<double>; }; %include "myvector.h" ---------------------------------- --myvector.h------------------- /* File : example.h */ #include <vector> #include <algorithm> #include <functional> #include <numeric> double average(std::vector<int> v) { return std::accumulate(v.begin(),v.end(),0.0)/v.size(); } std::vector<double> half(const std::vector<double>& v) { std::vector<double> w(v); for (unsigned int i=0; i<w.size(); i++) w[i] /= 2.0; return w; } void halve_in_place(std::vector<double>& v) { std::transform(v.begin(),v.end(),v.begin(), std::bind2nd(std::divides<double>(),2.0)); } ------------------------------------- Basicly they are just examples from the SWIG doc. And I ran: *swig -c++ -r -o myvector_wrap.cpp myvector.i PKG_LIBS="myvector.h" R CMD SHLIB myvector_wrap.cpp* Then in R I ran:>*dyn.load("myvector.so")* >*source("myvector.R")*Then i tried to create a vector in R:>*vi=IntVector(4)*I go the following error: *Error in .Call("R_swig_new_IntVector__SWIG_2", size, PACKAGE="myvector"): C symbol name "R_swig_new_IntVector__SWIG_2" not in DLL for package "myvector"* Since I basicly followed the steps in the doc, I don't understand where the error comes from. Can anybody help me out? Thanks a lot! Charlie [[alternative HTML version deleted]]
did you wrap your function prototype in extern "C" ? -Whit On Thu, Nov 6, 2008 at 1:16 PM, charlie <charlie.xia.fdu at gmail.com> wrote:> Hi, all > > I didn't get any response from swig for my question. > see if I can get some help here > > Thanks > > ---------- Forwarded message ---------- > From: charlie <charlie.xia.fdu at gmail.com> > Date: Tue, Nov 4, 2008 at 1:55 PM > Subject: SWIG with R and C++ STL > To: swig-user at lists.sourceforge.net > > > Hi all, > > I am new to SWIG. I encountered some problem when I try to SWIG to R some > C++ modules. > Here is the details. I got "myvector.i" and "myvector.h" as my two input > files, the contends are: > > ---myvector.i----- > %module myvector > %{ > #include "myvector.h" > %} > > %include "std_vector.i" > > namespace std { > %template(IntVector) vector<int>; > %template(DoubleVector) vector<double>; > }; > > %include "myvector.h" > ---------------------------------- > > --myvector.h------------------- > /* File : example.h */ > > #include <vector> > #include <algorithm> > #include <functional> > #include <numeric> > > double average(std::vector<int> v) { > return std::accumulate(v.begin(),v.end(),0.0)/v.size(); > } > > std::vector<double> half(const std::vector<double>& v) { > std::vector<double> w(v); > for (unsigned int i=0; i<w.size(); i++) > w[i] /= 2.0; > return w; > } > > void halve_in_place(std::vector<double>& v) { > std::transform(v.begin(),v.end(),v.begin(), > std::bind2nd(std::divides<double>(),2.0)); > } > ------------------------------------- > > Basicly they are just examples from the SWIG doc. > And I ran: > *swig -c++ -r -o myvector_wrap.cpp myvector.i > PKG_LIBS="myvector.h" R CMD SHLIB myvector_wrap.cpp* > Then in R I ran: >>*dyn.load("myvector.so")* >>*source("myvector.R")* > Then i tried to create a vector in R: >>*vi=IntVector(4)* > I go the following error: > *Error in .Call("R_swig_new_IntVector__SWIG_2", size, PACKAGE="myvector"): > C symbol name "R_swig_new_IntVector__SWIG_2" not in DLL for package > "myvector"* > > Since I basicly followed the steps in the doc, I don't understand where the > error comes from. > Can anybody help me out? > > Thanks a lot! > > Charlie > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
On Thu, Nov 6, 2008 at 11:29 AM, Whit Armstrong <armstrong.whit@gmail.com>wrote:> did you wrap your function prototype in extern "C" ? > > -Whit > > >Hi Whit, Thanks for replying my question. I already gave up my own code and now I am trying with the examples from SWIG Doc 1.3. I can not even reproduce one of their examples in section 5.4.9. I wonder if you can have a look at my code and figure out what's wrong there for me. I used the extern "C" this time and the details is as following: ---------test.i-------------- %module test %{ #include "test.h" %} %include "test.h" %callback("%s_cb"); int myadd( int, int ); //myadd_cb int mysub( int, int ); //mysub_cb int mymul( int, int ); //mymul_cb %nocallback; -------------------------------- -----------test.h-------------- extern "C"{ int binary_op(int a, int b, int (*op)(int,int) ); int myadd( int a, int b ) { return a+b; }; int mysub( int a, int b ) { return a-b; }; int mymul( int a, int b ) { return a*b; }; } --------------------------------- ------error message----------> dyn.load("test.so")Error in dyn.load("test.so") : unable to load shared library '/home/charlie/workspace/lla/test.so': /home/charlie/workspace/lla/test.so: undefined symbol: binary_op ---------------------------------- ------compiling message------ swig -v -r -c++ -o test_wrap.cpp test.i LangSubDir: r Search paths: ./ ./swig_lib/r/ /usr/local/share/swig/1.3.36/r/ ./swig_lib/ /usr/local/share/swig/1.3.36/ Preprocessing... Starting language-specific parse... Processing types... C++ analysis... Generating wrappers... Type: p.f(int,int).int Return type: int R CMD SHLIB test.cpp test_wrap.cpp make[1]: Entering directory `/home/charlie/workspace/lla' g++ -I/usr/local/lib/R/include -I/usr/local/lib/R/include -I/usr/local/include -fpic -g -O2 -c test_wrap.cpp -o test_wrap.o g++ -shared -L/usr/local/lib -o test.so test.o test_wrap.o make[1]: Leaving directory `/home/charlie/workspace/lla' ------------------------------------------ Many thanks! Charlie [[alternative HTML version deleted]]