编译python模块扩展时如何静态链接库 [英] How to statically link a library when compiling a python module extension
问题描述
我想修改setup.py文件,以便命令 python setup.py build编译基于C的扩展模块,该模块静态(而非动态)链接到库。
I would like to modify a setup.py file such that the command "python setup.py build" compiles a C-based extension module that is statically (rather than dynamically) linked to a library.
该扩展名当前动态链接到许多库。除了静态链接到一个库外,我想保留所有内容。我成功地通过手动修改了distutils运行的对gcc的调用来成功完成了此操作,尽管它要求我明确列出相关库。
The extension is currently dynamically linked to a number of libraries. I would like to leave everything unchanged except for statically linking to just one library. I have successfully done this by manually modifying the call to gcc that distutils runs, although it required that I explicitly listed the dependent libraries.
也许这是太多信息,但是为了清楚起见,这是在 python setup.py build脚本期间执行的最终链接命令:
Perhaps this is too much information, but for clarity this is the final linking command that was executed during the "python setup.py build" script:
gcc -pthread -shared -L/system/lib64 -L/system/lib/ -I/system/include build/temp.linux-x86_64-2.7/src/*.o -L/system/lib -L/usr/local/lib -L/usr/lib -ligraph -o build/lib.linux-x86_64-2.7/igraph/core.so
这是我的手动修改:
gcc -pthread -shared -L/system/lib64 -L/system/lib/ -I/system/include build/temp.linux-x86_64-2.7/src/*.o -L/system/lib -L/usr/local/lib -L/usr/lib /system/lib/libigraph.a -lxml2 -lz -lgmp -lstdc++ -lm -ldl -o build/lib.linux-x86_64-2.7/igraph/core.so
分发Python模块讨论了库的规范,但是只有 library_dirs是合适的,并且这些库是动态链接的。
Section 2.3.4 of Distributing Python Modules discusses the specification of libraries, but only "library_dirs" is appropriate and those libraries are dynamically linked.
我正在使用Linux环境进行开发,但是该软件包也将在Windows上编译和安装,因此我需要一种可移植的解决方案。
I'm using a Linux environment for development but the package will also be compiled and installed on Windows, so a portable solution is what I'm after.
可以有人告诉我在哪里寻找说明,或如何修改setup.py脚本? (提前感谢!)
Can someone tell me where to look for instructions, or how to modify the setup.py script? (Thanks in advance!)
我是StackOverflow的新手,所以我很抱歉,如果我没有正确标记这个问题,或者如果我在其中犯了一些其他错误,
I'm new to StackOverflow, so my apologies if I haven't correctly tagged this question, or if I have made some other error in this posting.
推荐答案
如果其他所有方法均失败,那么总会有文献记载很少的 extra_compile_args
和 extra_link_args $ c
扩展$ c的$ c>选项$ c>
构建器。 (另请参见此处。)
If all else fails, there's always the little-documented extra_compile_args
and extra_link_args
options to the Extension
builder. (See also here.)
您可能需要破解一些与操作系统相关的代码,以获取特定平台的正确参数格式。
You might need to hack in some OS-dependent code to get the right argument format for a particular platform though.
这篇关于编译python模块扩展时如何静态链接库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!