[Numpy-discussion] extension module with swig

Giovanni Samaey Giovanni.Samaey at cs.kuleuven.be
Tue Nov 4 05:10:26 EST 2008


Dear all,

I am unsure about the correct place to put this question -- If this  
isn't the correct list, please let me know which place is more  
appropriate.

I am trying to build an extension module in python that calls a C  
routine that depends on the GNU Scientific Library.
I am using swig to wrap the c-file and the numpy.i file that is  
included with numpy for the typemaps.  To build the module, I am using  
distutils, with a modified version of the setup.py file that was  
included with the examples (in order to link against gsl).

Although the build succeeds wtihout problems, I am getting "undefined  
symbol" errors from gsl when importing the module in python.  I am  
attaching the source code of the simplest possible example below  
together with the error messages.  Note that I am trying to link  
against my own gsl in my home directory.  There is no system-wide one  
available.

I get the feeling that I am doing something silly, but I am unable to  
put the finger on it.  Any help would be greatly appreciated.

As a c-file I have : test.c

#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
# include <math.h>

double var;
void test(double *x, double *y, double *z, int n) {
   int i;
     const gsl_rng_type *T;
     gsl_rng *r;

//    gsl_rng_env_setup();

//    T = gsl_rng_default;
     T = gsl_rng_mt19937;
     r = gsl_rng_alloc(T);
   for (i=0;i<n;i++) {
     *(z++)=x[i]+y[i]+var;
   }
   var += gsl_rng_uniform(r);
}

As a header file : test.h

extern double var;
void test(double *x, double *y, double *z, int n);


The swig interface file: test.i

/* test.i */
%module test

%{
#define SWIG_FILE_WITH_INIT
%}
%include "numpy.i"
%init %{
import_array();
%}

%inline %{
#include "test.h"
%}

%apply (double* IN_ARRAY1, int DIM1) {(double* x, int n1),
(double* y, int n2)}
%apply (double* ARGOUT_ARRAY1, int DIM1) {(double* z, int m)};

%rename (test) my_test;
%inline %{
void my_test(double* x, int n1, double* y, int n2, double* z, int m) {
test(x,y,z,m);
}
%}

%ignore test;
%include "test.h"

The setup.py file is :

#! /usr/bin/env python

# System imports
from distutils.core import *
from distutils      import sysconfig

# Third-party modules - we depend on numpy for everything
import numpy

# Obtain the numpy include directory.  This logic works across numpy  
versions.try:
     numpy_include = numpy.get_include()
except AttributeError:
     numpy_include = numpy.get_numpy_include()

# dot extension module
_test = Extension("_test",
                     ["test_wrap.c",
                      "test.c"],
                     include_dirs = [numpy_include,'/data/home/ 
u0038151/include'],
		    library_dirs = ['/data/home/u0038151/lib']
                   )

# NumyTypemapTests setup
setup(name        = ["test"],
       description = "test c module swigging",
       author      = "Giovanni Samaey",
       py_modules  = ["test"],
       ext_modules = [_test ]
       )

The error message is the following:

0 u0038151 at lo-03-02 dot2 $ python -c "import test"
Traceback (most recent call last):
   File "<string>", line 1, in ?
   File "test.py", line 7, in ?
     import _test
ImportError: ./_test.so: undefined symbol: gsl_rng_mt19937




More information about the NumPy-Discussion mailing list