Compilation failure with CMake due to missing libpython

Issue #20 resolved
Simone Pezzuto created an issue
from instant import inline

func = inline("double add(double a, double b){ return a+b; }",
              cache_dir = "test_cache",
              generate_setup = False)

fails.

This because the library is not linked against libpython.

In DOLFIN the problem doesn't show up because libpython is already included by DOLFINConfig.cmake.

A reasonable fix should be to add

find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_PATH})
swig_link_libraries(${SWIG_MODULE_NAME} ${PYTHON_LIBRARIES})

in CMakeLists.txt file, but this doesn't guarantee the consistency with the working python environment (sometimes PythonLibs picks up a different library when python is a non-standard location). A better solution could mimic what DOLFIN's CMakeLists.txt does to workaround this problem.

Comments (8)

  1. Johannes Ring

    You should supply the libraries you need to link against when calling inline. Something like this should work:

    func = inline("double add(double a, double b){ return a+b; }",
                  cache_dir="test_cache",
                  generate_setup=False,
                  include_dirs=["/somepath/include/python2.7"],
                  library_dirs=["/somepath/lib"]
                  libraries=["python2.7"])
    

    See also the documentation of instant.build.

  2. Simone Pezzuto reporter
    from instant import inline
    from distutils.sysconfig import get_config_var
    from os.path import splitext
    
    add_func = inline("double add(double a, double b){ return a+b; }",
            cache_dir = "test_cache",
            generate_setup = False,
            library_dirs = [ get_config_var('LIBDIR') ],
            include_dirs = [ get_config_var('INCLUDEPY') ],
            libraries = [ splitext(get_config_var('LIBRARY'))[0][3:] ])
    

    still doesn't work since apparently library_dirs is ignored by write_cmakefile in instant/codegeneration.py.

    Anyhow, I see your point, but I think that python libraries and headers should be always included automatically and not by the user. A minimal working example of CMake+SWIG must include PythonLibs to work, otherwise it won't compile. On the other hand, in this case all the files are generated by instant, so a plain PythonLibs doesn't seems the optimum because it can pick up the wrong library, usually ending up with a segmentation fault when the module is loaded (see also [1]).

  3. Simone Pezzuto reporter

    Sorry for the late answer ... actually it still doesn't work the pure instant example. But on DOLFIN the problem is fixed because of the commit you pointed out (the trick in that case is to keep track of the python libs/includes found during DOLFIN config).

    One possible fix for the instant counterpart is to copy&paste the ``find PythonLibs'' section in dolfin/CMakeLists.txt inside the instant-generated CMakeLists.txt.

    I fear you could face the same issue with Hashdist installation, with a user-compiled version of python ...

  4. Log in to comment