如何将 C lib 与 python 链接以在 Windows 下嵌入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1013441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to link C lib against python for embedding under Windows?
提问by Gregor
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.
我正在开发一个用 C 编写的应用程序。应用程序的一部分应该嵌入 python,这是我当前的问题。我尝试将我的源代码链接到 Python 库,但它不起作用。
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
.
当我使用 MinGW 时,我使用 dlltool 从 python26.lib 创建了 python26.a 文件,并将 *.a 文件放入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.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)
- 我的 Python“根”文件夹是
C:\Program Files (x86)\python\2.6
- Devsystem 是一个 Windows Server 2008
- 海湾合作委员会信息:
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
编辑:我忘了写关于我的 Python 安装的信息:这是官方的 python.org 安装 2.6.1
... and how I created the python.a file:
...以及我如何创建 python.a 文件:
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
回答by Nikokrock
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.
在 Windows 上,python 发行版已经libpython26.a
在 libs 子目录中带有一个,因此无需.a
使用 dll 工具生成文件。
I did try a little example with a single C file toto.c:
我确实尝试了一个带有单个 C 文件 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 :-)
它就像一个魅力。希望它会有所帮助:-)
回答by Jacob B
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?
Python(至少是我的发行版)带有一个“python-config”程序,可以为各种情况自动创建正确的编译器和链接器选项。但是,我从未在 Windows 上使用过它。也许这个工具可以帮助你?
回答by David Cournapeau
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.
IIRC,dlltool 并不总是有效。拥有 python 2.6 + Wow 使事情更不可能工作。对于 numpy,我是这样做的。基本上,我使用 obdump.exe 从 dll 构建表,我解析它以生成 .def。您应该检查您丢失的符号是否在 .def 中,否则它将无法工作。