so for my job I am using swig to pass an array through SWIG from a python file to a c++ file. I can get it to work, but not if it is inside a class. I am new to SWIG and I found the implementation here. If the array function is inside of my class I am getting this error. I want the function to be inside of the class because there are other functions I need to use to alter the array. The following code is a simplistic version of what I am trying to do, but the error is still the same and same issue applies. I would appreciate any guidance and help.
Traceback (most recent call last):
File "/root/Documents/SWIG_VECTOR/filter.py", line 1, in <module>
import test
File "/root/Documents/SWIG_VECTOR/test.py", line 15, in <module>
import _test
ImportError: /root/Documents/SWIG_VECTOR/_test.so: undefined symbol:
_ZN7myClass11print_arrayESt6vectorIdSaIdEE
test.i
%module test
%{
#include "test.hpp"
%}
%include "std_vector.i"
%template(Array) std::vector < double >;
class myClass {
public:
void print_array(const std::vector < double > myarray);
};
test.hpp
#ifndef TEST_HPP__
#define TEST_HPP__
#include <iostream>
#include <stdio.h>
#include <vector>
//#include "mySecondClass.hpp"
class myClass {
public:
void print_array(std::vector < double > myarray);
};
#endif /* TEST_HPP__ */
test.cpp
#include "test.hpp"
void print_array(std::vector < double > myarray)
{
for (int i =0; i < myarray.size(); ++i) {
std::cout << myarray.size() << " ";
}
}
I use these swig commands to compile.
swig -python -c++ -Isrc test.i
g++ -Isrc -fPIC -c $(pkg-config --cflags --libs python3) test.cpp test_wrap.cxx
g++ -shared -o _test.so test.o test_wrap.o