I'm developing a Python binding for a C++ library using Boost Python, for Linux and Windows (Visual Studio).
In Windows, the static Boost Python library has a dependency against Python (this is motive for another thread, here), so, in my CMake config I need to do:
if((${CMAKE_SYSTEM_NAME} STREQUAL "Linux") OR APPLE)
target_link_libraries(my_python_module ${Boost_LIBRARIES})
elseif(WIN32 AND MSVC)
add_definitions(/DBOOST_PYTHON_STATIC_LIB)
target_link_libraries(my_python_module ${Boost_LIBRARIES}) #This includes the Boost Python library
# Even though Boost Python library is included statically, in Windows it has a dependency to the Python library.
target_link_libraries(my_python_module ${Python_LIBRARIES})
endif()
This works fine in Linux, but in Windows, it only works in Release mode, not in Debug, in which case I always get a:
LINK : fatal error LNK1104: Can't open file 'python37.lib'
After some hair pulling I noticed the issue was caused by CMake instructing Visual Studio to link against 'python37_d.lib'
instead of 'python37.lib'
in the Debug mode.
However, as I described in the linked issue, the officially provided Boost Python debug library is linked against the Python release library, not the debug one. So, the solution would be to force the link against the Python release library, regardless of the build type. Unfortunately, ${Python_LIBRARIES}
sets the library automatically depending on the mode, and I wouldn't like to explicitly specify python37.lib in my code (since I can upgrade Python and I don't want to have to change my CMake scripts because of that).
I found some similar issues here and here, but that doesn't reflect the exact situation I'm facing. Based on those, I tried setting:
target_link_libraries(my_python_module optimized ${Python_LIBRARIES})
But that didn't work either. So, the question is:
Is there a way to force the usage of the Python release library in Debug mode WITHOUT having to set it explicitly and leaving the Python CMake package to do it automatically instead. By explicit I mean doing:
target_link_libraries(my_python_module python37)
Thanks a lot for your help.
FindBoost.cmake
here, you can set a variableBoost_USE_DEBUG_PYTHON
before callingfind_package(Boost ...)
. Try setting that variable toOFF
set(Boost_USE_DEBUG_PYTHON OFF)
. Does that help? – Basinpython37.lib
), not the debug one (python37_d.lib
). But, sinceFindPython.cmake
automatically sets the proper Python library for each build type, my code is trying to link againstpython37_d.lib
in debug mode, when I have to do it againstpython37.lib
. In fact, forcing the linkage againstpython37.lib
in debug fixes the issue, but that implies changing my CMake config and I'd prefer not. – PueblaBoost_USE_DEBUG_PYTHON
didn't fix the issue. – Pueblalibboost_python
and notlibpython
. Did you have a look atFindPython.cmake
? The variablePython_FIND_ABI
looks interesting (see here). Tryset(Python_FIND_ABI "OFF" "ANY" "ANY")
(will set debug toOFF
and malloc and unicode toANY
) before doingfind_package(Python)
. – Basin