Search code examples
pythondistutilspython-extensions

building python C++ extension -- how to know if a linked library cannot be found


I downloaded a python-wrapped C++ code and am trying to build it from source, and it compiles without errors, but when I run the end result, it fails in a way that seems to suggest that it did not find at least one of the libraries that it was supposed to link against.

What surprises me is, in distutils.core.Extension, you can give a list of libraries, but there is no error or message to tell me that one of the libraries does not exist. I can put any gibberish string in the list and it will still run without errors. Is there any setting for this in Extension? Or any other way to check?

For reference, here is the setup.py code (Ubuntu 14.04, Python 2.7):

coolmodule = Extension('cool',
    sources = [
        'cool/main_python.c'
    ],
    libraries = [
        'cool',
        'stdc++'
        'lapack',
        'blas',
        'gfortran',
        'fftw3',
        # if I add any gibberish string to this list,
        # it still runs without error!
    ],
    library_dirs = ['./build'],
    extra_link_args = [
        './build/libcool.a'
    ]
)

setup(name = 'cool',
    ext_modules = [coolmodule]
)

Thank you in advance!!


Solution

  • Are you creating a shared library that you import as a Python module? If so, you can use the ldd utility in Linux/Unix to see the shared libraries required by your module. On Mac OS X, the equivalent tool is otool -L

    (test)MacBook-Air-2:foo talumbau$ otool -L foo_ext.so
    foo_ext.so:
            /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1197.1.1)
            /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 2577.0.0)
    

    If the shared library requires other libraries and they aren't found, ldd or otool -L will show you which ones. You can then track them down and modify your LD_LIBRARY_PATH so that they will be linked it at load time.