title: Building Python extension module with distutils
body: <p>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 <code>extra_compile_args</code> property of the <code>Extension</code> object.  So I tried the following:</p>
  
  <pre><code>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])
  </code></pre>
  
  <p>This seems to compile, except when I import my module in Python it throws an <code>ImportError</code> 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 <code>distutils</code>, because if I add a compile argument that links to a bogus library name, distutils just compiles everything with no problem:</p>
  
  <pre><code>module.extra_compile_args = ['--std=c++0x', '-l some_fake_library'];
  </code></pre>
  
  <p>When I run <code>setup.py build</code>, the build runs with no errors!</p>
  
  <p>So, what's going on here?  How can I compile an extension module that requires linkage to a shared library?</p>