Search code examples
pythonswiggmp

swig + gmp library + compilation error on ubuntu 12.04


Here is my problem : I have to call an algo written in C with python. I use swig to do it.

So I have an example.i file which looks like this

%module example
%{
    #include "example.h"
%}
%include "example.h"

My example.h file is very simple

#ifndef EXAMPLE
#define EXAMPLE

#include <gmp.h>

// function that needs gmp library
void myFunction();

#endif

When I have to compile I run

swig -python example.i
gcc -fpic -std=c99 -c example.c example_wrap.c -I/usr/include/python2.7/    
gcc -shared example.o example_wrap.o -o -lgmp _example.so

Everything is fine so far, but then when I try to import example in python there is an error ImportError: example.so: undefined symbol: __gmpf_cmp

So there is something wrong with the link between swig and gmp library but I have no idea where the bug comes from.

python -V
2.7.3

swig -version
SWIG Version 2.0.4

ubuntu 12.04

Any help would be greatly appreciated.

Thanks in advance,

Victor


Solution

  • You seem to have mis-ordered the -lgmp flag so that it sits between -o and the library name. Probably would work better if you do:

    gcc -shared example.o example_wrap.o -lgmp -o _example.so