[SciPy-user] FYI: C++ Extensions for Python

Prabhu Ramachandran prabhu_r at users.sf.net
Tue Nov 2 04:40:34 EST 2004


>>>>> "DG" == David Grant <david.grant at telus.net> writes:

    DG> Prabhu Ramachandran wrote:
    >> This is a common thing you'd need to do with any other approach
    >> as well since all of them would link to your C++ code.  Plus,
    >> building a library out of .o's is straightforward.
    >>
    DG> Actually you're probably right.  I was trying to do it with
    DG> full automake/autoconf/configure support and that was what I
    DG> was getting bogged down in.  I guess making a library is the
    DG> same as is done with swig right?  Just call g++ with the
    DG> -shared argument?

Its easier with SCons.  Here is an untested example to give you a
taste:

# ------------------------------
import glob

ccflags = '-Wall -O3 -ggdb' # and whatever else
inc_dirs = ['.', 'sub_dir/']
lib_dirs = ['/usr/X11R6/lib', '.']

env = Environment(CCFLAGS = ccflags, CPPPATH=inc_dirs,
                  LIBPATH=lib_dirs)

# If you want to build with SWIG support use something like this
# instead.
env = Environment(CCFLAGS = ccflags, CPPPATH=inc_dirs,
                  LIBPATH=lib_dirs, SWIGFLAGS='-c++ -python ')

lib_src = glob.glob('src/*.c')
# Build a shared library from these sources.
env.SharedLibrary(target='my_library', source=lib_src)

# Now build main.c
libs = ['lapack', 'atlas', 'my_library']
env.Program(target='main', source='main.c', LIBS=libs)

# Then for a swig module:
env.SharedLibrary('_foo.so', 'foo.i', LIBS=libs)
# ------------------------------

Obviously there are more options, but this shows you the basics of a
library, executable and a swig shared module.

cheers,
prabhu




More information about the SciPy-User mailing list