[Rd] Fwd: SWIG with R and C++ STL

Whit Armstrong armstrong.whit at gmail.com
Thu Nov 6 20:29:25 CET 2008


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
>



More information about the R-devel mailing list