thewtex / cython-cmake-example

Utilities and example for using CMake to build Cython modules - migrated to scikit-build
https://scikit-build.org/
Apache License 2.0
160 stars 41 forks source link

Preventing linking against python lib #8

Closed justinfx closed 11 years ago

justinfx commented 11 years ago

First off, thanks for this example.

I have been trying to get this cmake setup working in a way that replicates the results I get from my distutils setup.py approach. When I use distutils, I end up with an .so that does not link agains a specific version of python.

When I build with the cmake setup, I end up with an .so that does link against a python library (and actually the wrong one as I am building within a virtualenv).

How can I prevent the cython cmake setup from linking against python to match the distutils approach?

As a bonus part to this question, I when I compile with distutils on OSX 10.7.5, doing no special environment flags, I get all of the source files compiles using gcc4.2. but then it links with c++ When I build the cmake project by default, it compiles with c++ and links with c++ ==> error When I build the cmake project with CXX=gcc it compiles with gcc and links with gcc ==> error The only way I get it to build is if I do CXX=g++ , which led me to an .so linked against python.

thewtex commented 11 years ago

To resolve all symbols, it is necessary to link again the python library. Specify which library is linked with the CMake configuration variable PYTHON_LIBRARY. It can be set by using 'cmake -D', editing the CMakeCache.txt file, ccmake, or cmake-gui.

Use the same compiler to build extensions that was used to build python. Specify it during the initial CMake configuration like

CC=gcc-4.2 CXX=g++-4.2 cmake /path/to/cython-cmake-example

Note that both CC (the C compiler) and CXX (the C++ compiler) should be specified.

justinfx commented 11 years ago

Yes but why doesn't the cython distutils .so show a link to libpython and the cmake one does?

On Mar 23, 2013, at 6:56 PM, Matt McCormick wrote:

To resolve all symbols, it is necessary to link again the python library. Specify which library is linked with the CMake configuration variable PYTHON_LIBRARY. It can be set by using 'cmake -D', editing the CMakeCache.txt file, ccmake, or cmake-gui.

Use the same compiler to build extensions that was used to build python. Specify it during the initial CMake configuration like

CC=gcc-4.2 CXX=g++-4.2 cmake /path/to/cython-cmake-example

Note that both CC (the C compiler) and CXX (the C++ compiler) should be specified.

— Reply to this email directly or view it on GitHub.

justinfx commented 11 years ago

After more research, I learned that a python extension library does not need to link against libpython. It can. But if it does not, it can be dynamically linked up by the python interpreter when it is loaded.

I made the following change to UseCython.cmake to get the functionality I was seeking:

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} )
  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()

... And called it with something like:

set(DYNAMIC_LOOKUP "TRUE")
cython_add_module( myModule DYNAMIC_LOOKUP ${src_files} )
thewtex commented 11 years ago

Cool. I will give it a try on Monday when I have access to all Mac/Linux/Windows platforms. Thanks for taking a look.

thewtex commented 11 years ago

Thanks for taking a look! I made -undefined dynamic_lookup the default for Mac's. Looks good for both Clang and GCC:

http://my.cdash.org/index.php?project=cython-cmake-example#

Thanks again!

justinfx commented 11 years ago

Fantastic! Thanks for following up on it.