views:

674

answers:

3

Hi,

I am working on an application written in C. One part of the application should embed python and there is my current problem. I try to link my source to the Python library but it does not work.

As I use MinGW I have created the python26.a file from python26.lib with dlltool and put the *.a file in C:/Program Files (x86)/python/2.6/libs.

Therefore, I compile the file with this command:

gcc -shared -o mod_python.dll mod_python.o "-LC:\Program Files (x86)\python\2.6\libs" -lpython26 -Wl,--out-implib,libmod_python.a -Wl,--output-def,mod_python.def

and I get those errors:

Creating library file: libmod_python.a
mod_python.o: In function `module_init':
mod_python.c:34: undefined reference to `__imp__Py_Initialize'
mod_python.c:35: undefined reference to `__imp__PyEval_InitThreads'
... and so on ...
  • My Python "root" folder is C:\Program Files (x86)\python\2.6
  • The Devsystem is a Windows Server 2008
  • GCC Information: Reading specs from C:/Program Files (x86)/MinGW/bin/../lib/gcc/mingw32/3.4.5/specs Configured with: ../gcc-3.4.5-20060117-3/configure --with-gcc --with-gnu-ld --with-gnu-as --host=mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable-languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shared --enable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-java-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchronization --enable-libstdcxx-debug Thread model: win32 gcc version 3.4.5 (mingw-vista special r3)

What I do wrong? How I get it compiled and linked :-)?

Cheers, gregor


Edit: I forgot to write information about my Python installation: It's the official python.org installation 2.6.1

... and how I created the python.a file:

dlltool -z python.def --export-all-symbols -v c:\windows\system32\python26.dll

dlltool --dllname c:\Windows\system32\python26.dll --def python.def -v --output-lib python26.a
+1  A: 

Python (at least my distribution) comes with a "python-config" program that automatically creates the correct compiler and linker options for various situations. However, I have never used it on Windows. Perhaps this tool can help you though?

Jacob B
As I see python-config is only available under *nix systems. But thank you for the idea. Now I can make my makefile better under those systems.
Gregor
+1  A: 

IIRC, dlltool does not always work. Having python 2.6 + Wow makes things even more less likely to work. For numpy, here is how I did it. Basically, I use obdump.exe to build the table from the dll, which I parse to generate the .def. You should check whether your missing symbols are in the .def, or otherwise it won't work.

David Cournapeau
You're right, the .def file does not include the missing functions. I try your way...
Gregor
... ok whatever I try I don't get the right entries in the .def file. Will try further
Gregor
+1  A: 

Well on Windows the python distribution comes already with a libpython26.a in the libs subdir so there is no need to generate .a files using dll tools.

I did try a little example with a single C file toto.c:

$ gcc -shared -o ./toto.dll ./toto.c -I/Python26/include/ -L/Python26/libs -lpython26

And it works like a charm. Hope it will help :-)

Nikokrock
Hmm... You're right. On my PC at work I've a `libpython26.a`. This must be new in the Python 2.6.2 distribution? Because at home with Python 2.6.1 I don't had it. Great. I must change your answer as the correct one!
Gregor
Short update: It works perfectly! Thank you all a lot.
Gregor