Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 469
Channel72 Avatar asked Jan 19 '23 22:01

Channel72


1 Answers

There's actually a special option for that.

For example:

libraries=["rt"]

You leave off the option and lib parts.

like image 182
Keith Avatar answered Jan 29 '23 12:01

Keith