Search code examples
c++perlxs

How to use PERL XS to call a C++ function that takes in a STL Vector


I have been using PerlXS to write a perl wrapper around a C++ Object. Usually my fcn takes in a string/int etc and I can just make them with no problem. I just write code like this in the .xs file

MyClass::func_a(std::string a, int b);

This time; I have a need to have a function that takes in a stl vector

MyClass::func_a(std::vector<std::string> vector)

I get this error:

conversion from `SV*' to non-scalar type 
  `std::vector<std::string, std::allocator<std::string> >'

Solution

  • You can't directly call the Native function which takes a STL container. You should write a wrapper and convert SV* manually to STL container.

    If You don't know how to do this (like I was), try to use SWIG http://www.swig.org/

    It can generate wrappers for a native funciton to use it from scripting languages (including PERL and XS-generator). The code from SWIG is not a very beautiful, also it has some limitations, but it is the easy way to write a wrapper.

    SWIG has a limited support of STL builtin: http://www.swig.org/Doc1.3/Library.html#Library_stl_cpp_library

    Also, for using PerlXS and vectors, check this thread http://www.mail-archive.com/[email protected]/msg00623.html