Building Python extension module with distutils

拥有回忆 提交于 2019-12-05 01:18:09

问题


I'm using distutils to build a Python extension module written in C++. The problem I have is that in order to compile the extension module, I need to link with a certain shared library. This requires setting an additional compiler flag. So, I searched through the Python docs and found out about the extra_compile_args property of the Extension object. So I tried the following:

from distutils.core import setup, Extension

module = Extension('test', sources = ['test.cpp'])
module.extra_compile_args = ['--std=c++0x', '-l mylib'];
setup(name = 'test', version = '1.0', ext_modules = [module])

This seems to compile, except when I import my module in Python it throws an ImportError exception due to an undefined symbol. So, apparently the library didn't link properly. So I tried writing a throw away C++ program which linked with the shared library, and it ran fine. Then I realized something really odd is going on with distutils, because if I add a compile argument that links to a bogus library name, distutils just compiles everything with no problem:

module.extra_compile_args = ['--std=c++0x', '-l some_fake_library'];

When I run setup.py build, the build runs with no errors!

So, what's going on here? How can I compile an extension module that requires linkage to a shared library?


回答1:


There's actually a special option for that.

For example:

libraries=["rt"]

You leave off the option and lib parts.




回答2:


One of the purposes of distutils is to make your code not dependent on a single compiler. Your "-l somelib" looks like it's meant to work with GCC (even though it should be "-lsomelib", no space). This is why you use the libraries option to the Extension class. Distutils will then pass the appropriate link command to whatever compiler it's using.

You can also look at the actual build commands distutils is using and execute them yourself to see what is going wrong.



来源:https://stackoverflow.com/questions/5760356/building-python-extension-module-with-distutils

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