Distutils vs cmake: link to libpython?

I asked another question that was too complicated for a direct answer, so I dropped it to this basic question ...

When I create my aModule.so using the standard cython distribution, it doesn't seem to be related to libpython :

 $ otool -L aModule.so aModule.so: /usr/local/lib/libboost_thread-mt.dylib (compatibility version 0.0.0, current version 0.0.0) /usr/local/opt/thrift/lib/libthrift-0.9.0.dylib (compatibility version 0.0.0, current version 0.0.0) /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11) 

But when I build with the cmake setting, it continues to create a linker command that links libpython to .so:

 $ otool -L aModule.so aModule.so: /System/Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.1) /usr/local/opt/thrift/lib/libthrift-0.9.0.dylib (compatibility version 0.0.0, current version 0.0.0) /usr/local/lib/libboost_thread-mt.dylib (compatibility version 0.0.0, current version 0.0.0) /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0) 

The module created by distutils seems to work just fine with any of my python2.7 installation (system or virtual file for my project). While cmake crashes with version mismatch when I try to import it using anything other than the associated system python.

Why does the distutils module work fine without being contacted? And if that is the case, why do I need cmake build to link libpython, and how could I prevent it, if so, so that it works with any of my python2.7 interpreters without crashing?

Currently I can direct cmake to the right python with: CXX=g++ cmake -DPYTHON_LIBRARY=/path/to/another/Python

+6
source share
1 answer

I realized that the source of the problem was related to cython-cmake-example and how its UseCython.cmake cython_add_module() explicitly links the library with libpython.

What I did for my own use, since I don't know if this is a fully portable solution, was to add a flag to this function to say DYNAMIC_LOOKUP :

 function( cython_add_module _name _dynamic_lookup ) set( pyx_module_sources "" ) set( other_module_sources "" ) foreach( _file ${ARGN} ) if( ${_file} MATCHES ".*\\.py[x]?$" ) list( APPEND pyx_module_sources ${_file} ) else() list( APPEND other_module_sources ${_file} ) endif() endforeach() compile_pyx( ${_name} generated_file ${pyx_module_sources} ) include_directories( ${PYTHON_INCLUDE_DIRS} ) python_add_module( ${_name} ${generated_file} ${other_module_sources} ) ### Added here ## if( ${_dynamic_lookup} ) message( STATUS "Not linking target ${_name} against libpython" ) set_target_properties( ${_name} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") else() target_link_libraries( ${_name} ${PYTHON_LIBRARIES} ) endif() endfunction() 

Now I can call cython_add_module and will not reference libpython.

+5
source

All Articles