简体   繁体   中英

Building Python extension module with distutils

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?

There's actually a special option for that.

For example:

libraries=["rt"]

You leave off the option and lib parts.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM