How to link C lib against python for embedding under Windows?

随声附和 提交于 2020-01-05 05:29:09

问题


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:


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 :-)




回答2:


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?




回答3:


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.



来源:https://stackoverflow.com/questions/1013441/how-to-link-c-lib-against-python-for-embedding-under-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!