linking between two modules with distutils
Question: I have recently [exposed a
problem](http://stackoverflow.com/questions/8443652/dependencies-between-
compiled-modules-in-python) when working with several compiled C++ modules and
would like to rephrase the question.

I have two modules 'mod1' and 'mod2'. They are compiled as two distinct
'ext_modules' in my setup.py, as shown here :

    
    
    #!/usr/bin/python2
    
    from setuptools import setup, Extension
    
    mod1 = Extension('mod1',
                     sources = ['mod1.cpp'],
                     libraries = ['boost_python'])
    
    mod2 = Extension('mod2',
                     sources = ['mod2.cpp'],
                     libraries = ['boost_python'])
    
    setup(name='foo',
          version='0.0',
          description='',
          ext_modules=[mod1,mod2],
          install_requires=['distribute'])
    

But internally, 'mod2.hpp' is including 'mod1.hpp', as the first module is
defining stuff that is used by the second module.

EDIT : this will compile fine, but then :

    
    
    $> cd build/lib.linux-i686-2.7
    $> python2 -c "import mod1 ; import mod2"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
    ImportError: ./mod2.so: undefined symbol: _ZN6ParentD2Ev
    

Here, "Parent" is the name of a class defined in mod1 and used in mod2.

EDIT2 : another weird behaviour I don't understand :

    
    
    $> cd build/lib.linux-i686-2.7
    $> python2
    Python 2.7.2 (default, Nov 21 2011, 17:24:32) 
    [GCC 4.6.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import mod2
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ImportError: ./mod2.so: undefined symbol: _ZN6ParentD2Ev
    >>> import mod1
    >>> import mod2
    Segmentation fault
    

Here, importing mod2 first fails. But if I try agin after importing mod1, I
get a segfault.

* * *

Both modules are built as shared libraries - but if I'm not mistaken, mod2
needs to be linked to mod1 in order to work, as it needs stuff defined in
mod1. Is it possible to define such a dependency with setuptools/distribute ?

Something like :

    
    
    mod2 = Extension('mod2',
                     sources = ['mod2.cpp'],
                     libraries = ['boost_python',mod1])
    

From my various readings, it looks like it is possible to do something like
this with boost's bjam utility - unfortunately, I didn't manage to use it
(even to compile the example) on my system.

Things I have tried:

  * adding 'mod1.cpp' to the sources of mod2. It works (kind of: I must import mod1 before mod2 to make it work) but I'm loosing the interest of having modules as shared objects.

Workarouds:

  * importing mod1 as a regular python module in mod2, but that would put an extra layer of python within my C++ code

What do you think ?


Answer: In the end, I could not find how to do that with distutils / distribute. So I
switched to cmake, and so far it works flawlessly !

with cmake, you can easily compile one module as a shared library, and later
add this shared library to the dependencies of another module.

