I am pretty new to SWIG and this is my first application in which I'm trying to make a generic List which is coded in C++ and trying to extend it in Python using SWIG
Code in list.h is as follows
#include <bits/stdc++.h>
template <typename T>
class List
{
public:
T* data;
int max_items;
int n_items;
List(int max);
void append(T d);
T get(int index);
};
typedef List<int> IntList;
typedef List<float> FloatList;
Code in list.cpp is as follows
#include "list.h"
template <class T> List<T>::List(int max) {
max_items = max;
data = new T[max_items];
n_items = 0;
}
template <class T> void List<T>::append(T d) {
if(n_items < max_items)
data[n_items++] = d;
}
template <class T> T List<T>::get(int index) {
return data[index];
}
And the SWIG input interface file list.i has following code
%module list_swig
%{
#include "list.h"
%}
template <class T>
class List {
public:
T* data;
int max_items;
int n_items;
List(int max);
void append(T data);
T get(int index);
};
template <class T> List<T>::List(int max) {
max_items = max;
data = new T[max_items];
n_items = 0;
}
template <class T> void List<T>::append(T d) {
if(n_items < max_items)
data[n_items++] = d
}
template <class T> T List<T>::get(int index) {
return data[index];
}
%template(IntPoint) List <int>;
%template(FloatPoint) List <float>;
I'm compiling as follows
swig -c++ -python -o list_wrap.cpp list.i
g++ -c -fPIC list.cpp
g++ -c -fPIC list_wrap.cpp -I/usr/include/python2.7
g++ -shared -o _list_swig.so list.o list_wrap.o
Every step produced output without any error but when I'm trying to import the file in python shell, it gives me following error
./_list_swig.so: undefined symbol: _ZN4ListIfE6appendE
Can anybody explain why this error occurred? Or where I'm making mistakes?
Thanks in advance!
Edit: The link to all the codes is https://github.com/b282022/SWIG-Experiment