0

I have some problem now with converting the c++ class into python code using swig.

This is my header file:

template<typename T>
class Dictionary {
    private:
        int size;
        T *array;
    public:
        Dictionary(int s);
        void set(int elem, T val);
        void get();
        ~Dictionary();
};

This is my cpp file:

template<typename T>
Dictionary<T>::Dictionary(int s) {
    this->size = s;
    this->array = new T[s];
}

template<typename T>
void Dictionary<T>::set(int elem, T val) {
    array[elem] = val;
}

template<typename T>
void Dictionary<T>::get() {
    for(int i=0; i<this->size; i++) {
        cout << i << array[i] << typeid(array[i]).name() << endl;
    }
}

template<typename T>
Dictionary<T>::~Dictionary() { }

And my interface file:

%module Dictionary
%{
  #include "Dictionary.h"
%}
%include <std_string.i>
%include <std_vector.i>
%include "stl.i"
%template(_string_list) std::vector< std::string >;
%include "Dictionary.h"
%template(dictint) Dictionary<int>;

The problem is that I always get an error like undefined symbols for architecture

Undefined symbols for architecture x86_64:
"Dictionary<int>::get()", referenced from:
  _wrap_dictint_get(_object*, _object*) in Dictionary_wrap.o
"Dictionary<int>::set(int, int)", referenced from:
  _wrap_dictint_set(_object*, _object*) in Dictionary_wrap.o
"Dictionary<int>::Dictionary(int)", referenced from:
  _wrap_new_DictionaryD(_object*, _object*) in Dictionary_wrap.o
"Dictionary<int>::~Dictionary()", referenced from:
  _wrap_delete_dictint(_object*, _object*) in Dictionary_wrap.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I wrote this command:

At first swig -c++ -python -py3 Dictionary.i

The second one is: g++ -g -ggdb -c -std=c++0x -I/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/include/python3.7m Dictionary.cpp Dictionary_wrap.cxx

And the third: gcc -dynamiclib -o _Dictionary.so *.o -L/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/ -lpython3.7 -flat_namespace

And on the third line, it crashed. Please help me

Akshit
  • 424
  • 4
  • 15
Lado
  • 276
  • 2
  • 17
  • I've never used SWIG, but as far as `c++` goes, are you aware that you need to implement the template definitions in the header file? Putting them in a `.cpp` will normally lead to undefined reference errors. – super Jun 23 '19 at 08:41
  • @super yeah you were right. Thank you very much. – Lado Jun 23 '19 at 08:51

0 Answers0