SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages.
- pre download the pcre package and put 'pcre-8.40.tar' in the root of swig directory
- run
./Tools/pcre-build.sh
- pre install automake
- ./autogen.sh
- ./configure & make & make install
/* File : example2.h */ #ifndef TEST_SWIG_LIBRARY_H #define TEST_SWIG_LIBRARY_H char* hello(); int m_mod(int n, int m); #endif
/* File : example2.c */ #include "example2.h" #include char * author = "moment"; char* hello() { return "moment's module"; } int m_mod(int n, int m) { return(n % m); }
/* File : example2.i */ %module example2 %{ /* Put headers and other declarations here */ extern char* author; extern char* hello(); extern int m_mod(int n, int m); %} extern char* author; extern char* hello(); extern int m_mod(int n, int m);
swig -python example2.i
gcc -fPIC -c example2_wrap.c -I ~/dev/py_venv/test_swig/include/python2.7
export DYLD_LIBRARY_PATH='/Users/moment/CLionProjects/swig_example/'
gcc -lpython2.7 -dynamiclib ../output/lib/libexample2.dylib example2_wrap.o -o _example2.so