在Windows/Visual Studio中从CMake强制以调试模式链接到Python发布库 [英] Force linking against Python release library in debug mode in Windows/Visual Studio from CMake

查看:139
本文介绍了在Windows/Visual Studio中从CMake强制以调试模式链接到Python发布库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Boost Python为Linux和Windows(Visual Studio)开发C ++库的Python绑定.

I'm developing a Python binding for a C++ library using Boost Python, for Linux and Windows (Visual Studio).

在Windows中,静态Boost Python库具有针对Python的依赖关系(这是另一个线程的动机,

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

这在Linux上工作正常,但是在Windows中,它仅在Release模式下有效,而在Debug下不起作用,在这种情况下,我总是得到:

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'

在拔掉头发后,我注意到问题是由CMake指示Visual Studio在调试模式下链接到 'python37_d.lib' 而不是 '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.

但是,正如我在 Boost Python debug 库与Python release 库而不是调试库链接.因此,解决方案将是针对构建版本强制使用针对Python发布库的链接.不幸的是, ${Python_LIBRARIES} 会根据模式自动设置库,并且我不想在代码中显式指定python37.lib(因为我可以升级Python并且我不想拥有因此更改我的CMake脚本.)

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:

有没有一种方法可以强制在调试模式 WITHOUT 中使用Python发布库,而不必明确设置它,而让Python CMake包自动完成它.明确地说,我的意思是:

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)

非常感谢您的帮助.

推荐答案

似乎kanstar注释中建议的set(Python_FIND_ABI "OFF" "ANY" "ANY")是执行此操作的正确方法.但是,当Python_FIND_ABI在CMake中时, master ,它尚未在

It seems that set(Python_FIND_ABI "OFF" "ANY" "ANY") as suggested in the comments by kanstar would be the correct way to do this. However, while Python_FIND_ABI is in CMake master, it hasn't been released yet in the latest version (v3.15.2 as of this writing).

同时,有一些解决方案取决于CMake版本.

In the meantime, there are solutions dependent on the CMake version.

CMake 3.12及更高版本

可以链接到 FindPython Python_LIBRARY_RELEASE,这并不意味着它是模块的

It's possible to link against FindPython's Python_LIBRARY_RELEASE, which isn't meant to be part of the module's public interface, but the variable is set correctly nonetheless.

cmake_minimum_required (VERSION 3.12)
find_package(Python ..<choose your COMPONENTS; refer to FindPython docs>..)
if(WIN32 AND MSVC)
  target_link_libraries(my_python_module ${Python_LIBRARY_RELEASE})
endif()

CMake 3.0.4至3.11

感谢@Phil的评论,我们可以扩展答案以包括具有

Thanks to a comment by @Phil, we can expand the answer to include earlier CMake versions which had the FindPythonLibs module that sets the PYTHON_LIBRARY_RELEASE variable instead.

cmake_minimum_required (VERSION 3.0)
find_package(PythonLibs ..<refer to FindPythonLibs docs>..)
if(WIN32 AND MSVC)
  target_link_libraries(my_python_module ${PYTHON_LIBRARY_RELEASE})
endif()

这篇关于在Windows/Visual Studio中从CMake强制以调试模式链接到Python发布库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆