Wrapping a C++ class in Python using SWIG
Solution 1
I think the swig command should be "swig -c++ -python example.swig"
Solution 2
There's not enough information here to be sure what's wrong, but I have two ideas for things you can try.
Your
g++
invocation is compiling a C source file as if it were C++. This is not guaranteed to work. Try insteadgcc -I/usr/local/include/python2.6 -fPIC -c example_wrap.c gcc -I/usr/local/include/python2.6 -fPIC -c example.cpp g++ -shared example_wrap.o example.o -o example.so
(yes, srsly, only use g++ for the link)
If that doesn't work, compile
example_wrap.c
like this:gcc -I/usr/local/include/python2.6 -fPIC -c -save-temps example_wrap.c
That will fail the same way but will produce a file named example_wrap.i
which is the result of preprocesing. It will be gigantic. Search that file for the function Swig_var_Math_get
, and add to your question the complete text of that function (but nothing else).
Ajay
Updated on July 18, 2022Comments
-
Ajay almost 2 years
example.h:
#ifndef EXAMPLE_H #define EXAMPLE_H class Math { public: int pi() const; void pi(int pi); private: int _pi; }; #endif
example.cpp:
#include "example.h" int Math::pi() const { return this->_pi; } void Math::pi(int pi) { this->_pi = pi; }
example.swig:
%module example %{ #define SWIG_FILE_WITH_INIT #include "example.h" %} %include "example.h"
I then generate the wrappers, "example.py" and "example_wrap.c" using:
swig -python example.swig
When I try to compile the wrapper class using:
g++ -fPIC -c example.cpp example_wrap.c -I/usr/local/include/python2.6/
I get the following error:
example_wrap.cpp: In function "PyObject* Swig_var_Math_get()": example_wrap.cpp:2725: error: expected primary-expression before "void" example_wrap.cpp:2725: error: expected ")" before "void"
The Error is at the following line :
pyobj = SWIG_NewPointerObj(SWIG_as_voidptr(&Math), SWIGTYPE_p_class, 0 ); #define SWIG_as_voidptr(a) (void *)((const void *)(a))
Is it the right way to generate the wrapper class "example_wrap.c"?
-
jweyrich over 13 yearsOops, example.cpp should be compiled with g++. Why gcc? Avoid C++ mangling?
-
zwol over 13 yearsIt is only necessary to use
g++
to link.gcc -c
does the Right Thing when applied to a.cpp
source file. Butg++ -c
does not do the Right Thing when applied to a.c
source file. So the easiest rule to remember is, always usegcc
to compile, even if the source is C++; only useg++
to link a program that contains C++. Same same for any of the other languages supported by GCC: use the language-specific driver only to link. Personally I don't think the language-specific drivers should even exist, but they do let you not have to know exactly how to call for the runtime libraries. -
jweyrich over 13 yearswell, yes, you can use gcc to compile C++. You could also use gcc to link C++, but you'd have to manually link against libstdc++, etc. Put that way, I find easier to use gcc for C, and g++ for C++. But it's just personal taste. The important is that problem & solution were correctly pointed out. +1