first time use of swig, python and c++ .. it's a mess ... please advice

Hyuga hyugaricdeau at gmail.com
Wed Feb 27 10:00:50 EST 2008


On Feb 26, 3:38 pm, Eric von Horst <z80vsvi... at hotmail.com> wrote:
> Hi,
>
> we have a third-party product that has a C++ api on HP-UX.
>
> I would like be able to use the API in Python (as I remember Python is
> good at doing this).
>
> I have no experience with this so I Googled and tried to find some
> info on what I had to do.
>
> So, I installed Python 2.4.4 and Swig 1.3.33
>
> The header file to the library is '/opt/OV/include/opcsvcapi.h'.
>
> I created a SWIG file with the following content:
> "
> %module opcsvcapi
>  %{
>  /* Includes the header in the wrapper code */
>  #include "/opt/OV/include/opcsvcapi.h"
>  %}
>
>  /* Parse the header file to generate wrappers */
>  %include "/opt/OV/include/opcsvcapi.h"
> "
> Then I ran the cmd:
> # swig -c++ -python opcsvcapi.i
> with output:
> "
> /opt/OV/include/opcsvcapi.h:41: Warning(362): operator= ignored
> /opt/OV/include/opcsvcapi.h:46: Warning(503): Can't wrap 'operator
> Type' unless renamed to a valid identifier.
> "
>
> The result are two files:
> opcsvcapi.py
> opcsvcapi_wrap.cxx
>
> I created a 'setup.py' file with the following content:
> "
> import distutils
> from distutils.core import setup, Extension
>
> setup(name = "opcsvcapi",
>       version = "1.0",
>       ext_modules = [Extension("_opcsvcapi",
> ["opcsvcapi.i","opcsvcapi.cxx"])])
> "
>
> Then I run: python setup.py build
>
> This results in an extra file:
> opcsvcapi_wrap.c and a 'build' directory
>
> and the following errors:
> "
> running build
> running build_ext
> building '_opcsvcapi' extension
> swigging opcsvcapi.i to opcsvcapi_wrap.c
> swig -python -o opcsvcapi_wrap.c opcsvcapi.i
> creating build
> creating build/temp.hp-ux-B.11.11-9000-800-2.4
> gcc -pthread -fno-strict-aliasing -DNDEBUG -g -O3 -Wall -Wstrict-
> prototypes -fPIC -I/usr/local/include/python2.4 -c opcsvcapi_wrap.c -o
> build/temp.hp-ux-B.11.11-9000-800-2.4/opcsvcapi_wrap.o
> In file included from /usr/local/include/python2.4/Python.h:8,
>                  from opcsvcapi_wrap.c:118:
> /usr/local/include/python2.4/pyconfig.h:851:1: warning:
> "_POSIX_C_SOURCE" redefined
> <command line>:1:1: warning: this is the location of the previous
> definition
> In file included from opcsvcapi_wrap.c:2490:
> /opt/OV/include/opcsvcapi.h:12:18: error: vector: No such file or
> directory
> In file included from opcsvcapi_wrap.c:2490:
> /opt/OV/include/opcsvcapi.h:15: error: expected '=', ',', ';', 'asm'
> or '__attribute__' before 'SvcAPI'
> opcsvcapi_wrap.c: In function 'Swig_var_SvcAPI_set':
> opcsvcapi_wrap.c:2505: error: 'SvcAPI' undeclared (first use in this
> function)
> opcsvcapi_wrap.c:2505: error: (Each undeclared identifier is reported
> only once
> opcsvcapi_wrap.c:2505: error: for each function it appears in.)
> opcsvcapi_wrap.c:2505: error: 'namespace' undeclared (first use in
> this function)
> opcsvcapi_wrap.c:2505: error: expected expression before ')' token
> opcsvcapi_wrap.c: In function 'init_opcsvcapi':
> opcsvcapi_wrap.c:3064: error: 'Swig_var_SvcAPI_get' undeclared (first
> use in this function)
> error: command 'gcc' failed with exit status 1
> "
>
> and that's it.
>
> Any idea what I am doing wrong?
>
> Any help much appreciated
>
> Kris

Well, the errors you got from the compiler are just confusing to me,
but I suspect it goes back to the warning you got from SWIG.  Some
class in there is overriding the = operator, but since that can't be
done in Python, you need to give it some special instructions on how
to handle that.

For example, if you have Foo::operator=, in your SWIG header include a
line like:
%rename(assign) Foo::operator=

Then in the Python module, the Foo class will have an assign method.

Hyuga



More information about the Python-list mailing list