[Numpy-discussion] Calling routines from a Fortran library using python

Dag Sverre Seljebotn dagss@student.matnat.uio...
Thu Feb 18 08:56:37 CST 2010

Nils Wagner wrote:
> On Thu, 18 Feb 2010 15:32:12 +0100
>   Dag Sverre Seljebotn <dagss@student.matnat.uio.no> 
> wrote:
>> David Cournapeau wrote:
>>> On Thu, Feb 18, 2010 at 10:22 PM, Nils Wagner
>>> <nwagner@iam.uni-stuttgart.de> wrote:
>>>> On Thu, 18 Feb 2010 11:55:07 +0100
>>>>  Matthieu Brucher <matthieu.brucher@gmail.com> wrote:
>>>>>> Ok I have extracted the *.o files from the static
>>>>>> library.
>>>>>> Applying the file command to the object files yields
>>>>>> ELF 64-bit LSB relocatable, AMD x86-64, version 1
>>>>>> (SYSV),
>>>>>> not stripped
>>>>>> What's that supposed to mean ?
>>>>> It means that each object file is an object file
>>>>> compiled with -fPIC,
>>>>> so you just have to make a shared library (gfortran
>>>>> -shared *.o -o
>>>>> libmysharedlibrary.so)
>>>>> Then, you can try to open the library with ctypes. If
>>>>> something is
>>>>> lacking, you may have to add -lsome_library to the
>>>>> gfortran line.
>>>>> Matthieu
>>>>> --
>>>>> Information System Engineer, Ph.D.
>>>>> Blog: http://matt.eifelle.com
>>>>> LinkedIn: http://www.linkedin.com/in/matthieubrucher
>>>> O.k. I tried
>>>> gfortran -shared *.o -o libmysharedlibrary.so
>>>> /usr/bin/ld: dxop.o: relocation R_X86_64_32 against `a
>>>> local symbol' can not be used when making a shared 
>>>> object;
>>>> recompile with -fPIC
>>> The message is pretty explicit: it is not compiled with 
>>> -fPIC, there
>>> is nothing you can do, short of requesting a shared 
>>> library from the
>>> software vendor.
>> Well, I think one can make a static executable with C or 
>> Cython and 
>> embed the Python interpreter. But it is pretty 
>> complicated stuff, and 
>> requesting a shared library is vastly preferable.
>> Dag Sverre
> Can you shed light on your approach ?

If one searches the Cython lists (gmane.org) for "embedding python 
interpreter" it should give some hints as to how to compile a Cython 
.pyx module into an executable (so you get an executable which links in 
Python, and which has to be used instead of Python). There's even some 
flags in Cython to do this easily. Ask on the Cython list for more info, 
I don't know more myself.

Then, one could link the static Fortran library into the resulting 
application statically, and use Cython to call the exported functions in 
the Fortran library.

But, the result is a standalone application, one can't use it with the 
standard Python interpreter (although one can import in any .py files 
etc. as usual).

Dag Sverre

More information about the NumPy-Discussion mailing list