How do I pass structures using a C extension?

timothy.williams at nvl.army.mil timothy.williams at nvl.army.mil
Mon Mar 14 13:42:03 EST 2005


Thanks for all the replies so far.  I'm starting to look at SWIG, but
the libraries I want access to are all static. I created a small
interface file and a setup.py file, but when I build it, I get
undefined symbols.  It sounds like the pack/unpack struct method is a
little to messy for my tastes. I was looking at the example for
defining a new type, but I wasn't sure how to pass the structure. I
need to look more into the PyArg_ParseTupleAndKeywords and creating a
list to pass into that.

I haven't looked at the ctypes module yet, but it looks like it not one
of the modules Python comes with, so I'm a little reluctant to use it.

Here's my set.py and interface file for my module:

##### setup.py ########
#!/bin/env python
import sys, os
from distutils.core import setup, Extension

OTB_HOME='/vps/otbknox/williams/OTB_2.0'
OTB_INCLDIR=[
    os.path.join(OTB_HOME, 'include', 'global'),
    os.path.join(OTB_HOME, 'include', 'libinc'),
    os.path.join(sys.prefix,'include','python2.3'),
    OTB_HOME
    ]
OTB_LIBDIR=[os.path.join(OTB_HOME, 'lib'),
            os.path.join(sys.prefix, 'lib', 'python2.3')]
OTB_LIBS=['cmdline']

setup (name = "OTB_cmdline",
       version="1.0",
       author="Tim Williams",
       ext_modules=[Extension('_OTB_cmdline',
                              sources=['OTB_cmdline.i'],
                              include_dirs=OTB_INCLDIR,
                              library_dirs=OTB_LIBDIR,
                              libraries=OTB_LIBS,

runtime_library_dirs=[os.path.join(OTB_HOME,'lib')]
                              )]
       )
#############

##### module.i ##########
%module OTB_cmdline
%{
#include <libcmdline.h>
#include <stdext.h>
#include <stdio.h>
%}

extern void                     cmd_process_options (
    int argc,
    argv_t argv,
    int *leftover_argc,
    argv_t * leftover_argv,
    CMD_OPTION * options,
    uint32 sizeof_options,
    int32 verbose);

###############

Here's the output of the build:
####################
python setup.py build --force
running build
running build_ext
building '_OTB_cmdline' extension
swigging OTB_cmdline.i to OTB_cmdline_wrap.c
swig -python -o OTB_cmdline_wrap.c OTB_cmdline.i
/usr/bin/gcc33 -fno-strict-aliasing -DNDEBUG -g -O3 -Wall
-Wstrict-prototypes -fPIC
-I/vps/otbknox/williams/OTB_2.0/include/global
-I/vps/otbknox/williams/OTB_2.0/include/libinc
-I/project/c4i/Users_Share/williams/Linux/include/python2.3
-I/vps/otbknox/williams/OTB_2.0
-I/project/c4i/Users_Share/williams/Linux/include/python2.3 -c
OTB_cmdline_wrap.c -o build/temp.linux-i686-2.3/OTB_cmdline_wrap.o
OTB_cmdline_wrap.c:187: warning: `SWIG_Python_TypeDynamicCast' defined
but notused
OTB_cmdline_wrap.c:199: warning: `SWIG_Python_TypeName' defined but not
used
OTB_cmdline_wrap.c:205: warning: `SWIG_Python_TypeQuery' defined but
not used
OTB_cmdline_wrap.c:444: warning: `SWIG_Python_addvarlink' defined but
not used
OTB_cmdline_wrap.c:551: warning: `SWIG_Python_MustGetPtr' defined but
not used
OTB_cmdline_wrap.c:559: warning: `SWIG_Python_ConvertPacked' defined
but not used
gcc -pthread -shared build/temp.linux-i686-2.3/OTB_cmdline_wrap.o
-L/vps/otbknox/williams/OTB_2.0/lib
-L/project/c4i/Users_Share/williams/Linux/lib/python2.3-Wl,-R/vps/otbknox/williams/OTB_2.0/lib
-lcmdline -o build/lib.linux-i686-2.3/_OTB_cmdline.so
################

After copying the _OTB_cmdline.so file to the directory where
OTB_cmdline.py  is,  here's what happens when I try to import it:

#############
Python 2.3.2 (#10, Feb 24 2005, 10:59:54)
[GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import  OTB_cmdline
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "OTB_cmdline.py", line 5, in ?
    import _OTB_cmdline
ImportError: ./_OTB_cmdline.so: undefined symbol: cmd_process_options
################

Can I only make extensions with shared libraries?




More information about the Python-list mailing list