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

Discussion in 'Python' started by Eric von Horst, Feb 26, 2008.

  1. 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
     
    Eric von Horst, Feb 26, 2008
    #1
    1. Advertising

  2. Eric von Horst

    Hyuga Guest

    On Feb 26, 3:38 pm, Eric von Horst <> 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::eek:perator=, in your SWIG header include a
    line like:
    %rename(assign) Foo::eek:perator=

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

    Hyuga
     
    Hyuga, Feb 27, 2008
    #2
    1. Advertising

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

    The operator= stuff is usually innocuous.
    The compiler died because it couldn't find 'vector', which is reasonable, since it thought it was compiling a C file.

    Probably because you swigged the file without the magic "-c++" option -- I'm not sure how distutils passes arguments to swig, but if you do it from the command line, you'll do something like swig -c++ -python -I<path to python> <file> and it will generate a .cpp file and a .py file, which will (hopefully) compile.

    I'd focus on doing it manually, then getting distutils to work properly.





    -----Original Message-----
    From: Hyuga [mailto:]
    Sent: Wednesday, February 27, 2008 10:01 AM
    To:
    Subject: Re: first time use of swig, python and c++ .. it's a mess ... please advice

    On Feb 26, 3:38 pm, Eric von Horst <> 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::eek:perator=, in your SWIG header include a line like:
    %rename(assign) Foo::eek:perator=

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

    Hyuga

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice.

    --------
    IRS Circular 230 Disclosure:
    Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.
     
    Bronner, Gregory, Feb 27, 2008
    #3
  4. On Feb 27, 9:44 pm, "Bronner, Gregory" <>
    wrote:
    > The operator= stuff is usually innocuous.
    > The compiler died because it couldn't find 'vector', which is reasonable, since it thought it was compiling a C file.
    >
    > Probably because you swigged the file without the magic "-c++" option -- I'm not sure how distutils passes arguments to swig, but if you do it from the command line, you'll do something like swig -c++ -python -I<path to python> <file> and it will generate a .cpp file and a .py file, which will (hopefully) compile.
    >
    > I'd focus on doing it manually, then getting distutils to work properly.
    >
    > -----Original Message-----
    > From: Hyuga [mailto:]
    > Sent: Wednesday, February 27, 2008 10:01 AM
    > To:
    > Subject: Re: first time use of swig, python and c++ .. it's a mess ... please advice
    >
    > On Feb 26, 3:38 pm, Eric von Horst <> 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::eek:perator=, in your SWIG header include a line like:
    > %rename(assign) Foo::eek:perator=
    >
    > Then in the Python module, the Foo class will have an assign method.
    >
    > Hyuga
    >
    > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    >
    > This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice.
    >
    > --------
    > IRS Circular 230 Disclosure:
    > Please be advised that any discussion of U.S. tax matters contained within this communication (including any attachments) is not intended or written to be used and cannot be used for the purpose of (i) avoiding U.S. tax related penalties or (ii) promoting, marketing or recommending to another party any transaction or matter addressed herein.


    That is indeed what I did.

    A do get .cpp file. The thin is that if I run setup, the library is
    again swigged but without the -c++ option (see original post)

    but honestly, I don't know what to do after I have the .cpp file.

    I could not find any docs that explained clearly how to compile if you
    are using C++

    The only thing I found was that it was not so straightforward

    So, again, any help is very much appreciated
     
    Eric von Horst, Feb 28, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Sheehan

    SWIG and Python in use?

    Alan Sheehan, Jul 7, 2003, in forum: Python
    Replies:
    1
    Views:
    323
    Wolfgang
    Jul 8, 2003
  2. Gary Wilson Jr

    trying to use swig for the first time

    Gary Wilson Jr, Jan 23, 2006, in forum: Python
    Replies:
    0
    Views:
    301
    Gary Wilson Jr
    Jan 23, 2006
  3. flamesrock
    Replies:
    8
    Views:
    486
    Hendrik van Rooyen
    Nov 24, 2006
  4. Gilles Ganault

    Does Python mess with CRLFs?

    Gilles Ganault, Nov 12, 2008, in forum: Python
    Replies:
    4
    Views:
    307
    Irmen de Jong
    Nov 12, 2008
  5. Roy Smith
    Replies:
    1
    Views:
    817
    Gabriel Genellina
    Dec 8, 2009
Loading...

Share This Page