我一定犯了某种明显的错误,但经过几个小时的战斗,我无法取得进一步的进展:
升级到 Boost 1.54、CMake 2.8.12 和 Python 2.7.5(这三个版本都来自稍早的次要版本)后,我的项目的 Python 绑定不再在调试配置中链接(它们在发布中链接正常)。我正在使用VS 2012进行构建。在更新之前一切正常。
我以标准方式构建了 Boost:bootstrap.bat
其次是 b2 address-model=64 toolset=msvc-11.0
。我的系统有一个 Python 2.7 安装,由 b2 选择:
notice: [python-cfg] Configuring python...
notice: [python-cfg] Registry indicates Python 2.7 installed at "C:Python27"
notice: [python-cfg] Checking interpreter command "python"...
notice: [python-cfg] running command 'DIR /-C /A:S "C:Python27python.exe" 2>&1'
notice: [python-cfg] running command 'python -c "from sys import *; print('version=%d.%dnplatform=%snprefix=%snexec_prefix=%snexecutable=%s' % (version_info[0],version_info[1],platform,prefix,exec_prefix,executable))" 2>&1'
notice: [python-cfg] ...requested configuration matched!
notice: [python-cfg] Details of this Python configuration:
notice: [python-cfg] interpreter command: "python"
notice: [python-cfg] include path: "C:Python27Include"
notice: [python-cfg] library path: "C:Python27libs"
notice: [python-cfg] DLL search path: "C:Python27"
我的机器上没有任何其他 Python 安装。
当我在项目上运行 CMake 时,一切看起来也不错:
Found PythonLibs: optimized;C:/Python27/libs/python27.lib;debug;C:/Python27/libs/python27_d.lib (found version "2.7.5")
Debug 中链接器命令行的相关部分符合预期:
"C:franzdevboost_1_54_0stageliblibboost_python-vc110-mt-gd-1_54.lib" "C:Python27libspython27_d.lib"
当我最终在调试中构建项目时:
LINK : fatal error LNK1104: cannot open file 'python27.lib'
由于链接器的命令行上没有任何地方python27.lib
提及,因此我使用十六进制编辑器编辑了libboost_python-vc110-mt-gd-1_54.lib
,只是发现它包含对python27.lib
(形式为 /DEFAULTLIB:"python27.lib"
)的引用,而我本期望引用python27_d.lib
而不是(其中没有)。
我在构建 Boost 时做错了什么吗?这是 Boost 1.54 中 Boost.Python 的已知问题吗?任何帮助将不胜感激。
更新#1:我再次尝试使用Boost 1.51和1.50,但出现了同样的问题,所以这不是Boost中的回归。
更新#2:我从Python安装中删除了Python lib(python27_d.lib)的调试版本,从而恢复到vanilla Python安装。然后,我重建了 Boost 1.51 和我的项目(CMake 按预期报告了一个库文件:Found PythonLibs: C:/Python27/libs/python27.lib (found version "2.7.5")
)。问题仍然存在,但是错误消息现在提到 python27_d.lib:LINK : fatal error LNK1104: cannot open file 'python27_d.lib'
!
更新#3:使用进程监视器,我可以在实际驻留的C:Python27libs
中搜索python27_d.lib:
3:35:28.0550683 PM link.exe 10132 CreateFile C:franzdevappleseedbuildappleseed.pythonpython27_d.lib NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
3:35:28.0551846 PM link.exe 10132 CreateFile C:franzdevboost_1_50_0stagelibpython27_d.lib NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
3:35:28.0552474 PM link.exe 10132 CreateFile C:franzdevboost_1_50_0stagelibDebugpython27_d.lib PATH NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
3:35:28.0553595 PM link.exe 10132 CreateFile C:franzdevappleseedbuildappleseed.pythonpython27_d.lib NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
3:35:28.0556105 PM link.exe 10132 CreateFile C:Program Files (x86)Microsoft Visual Studio 11.0VClibamd64python27_d.lib NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
3:35:28.0559637 PM link.exe 10132 CreateFile C:Program Files (x86)Microsoft Visual Studio 11.0VCatlmfclibamd64python27_d.lib NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
3:35:28.0560984 PM link.exe 10132 CreateFile C:Program Files (x86)Windows Kits8.0Libwin8umx64python27_d.lib NAME NOT FOUND Desired Access: Read Attributes, Disposition: Open, Options: Open Reparse Point, Attributes: n/a, ShareMode: Read, Write, Delete, AllocationSize: n/a
3:35:28.0561741 PM link.exe 10132 CreateFile C:franzdevappleseedbuildappleseed.pythonpython27_d.lib NAME NOT FOUND Desired Access: Generic Read, Disposition: Open, Options: Synchronous IO Non-Alert, Non-Directory File, Attributes: n/a, ShareMode: Read, AllocationSize: n/a
更新 #4:相关问题:可视化C++调试库命名约定
我解决了这个问题,这要归功于这篇文章中的提示:Visual C++调试库命名约定。
基本上,Python(C:Python27include
)附带的头文件pyconfig.h
强制链接到调试构建中的python27_d.lib
(通过#pragma comment()
指令),无论此库是否存在。
诀窍是永远不要直接包含Python.h
,而是包含该文件的 Boost 包装器,boost/python/detail/wrap_python.hpp
它负责禁用有问题的 #pragma comment()
指令。
包含boost/python/detail/wrap_python.hpp
而不是Python.h
允许您使用Python的发布版本,即使在构建程序的调试版本时也是如此。
构建一个 Boost 的调试版本,该版本链接到您的 Python 调试版本。(我使用的是VS2013,但过程应该与VS2010和VS2012相同)。
首先创建一个文本文件,例如,my_config.bjam
conatining:
using python : 2.7 #version
: C:\Python-2.7.10-64bit-dbg-msvc12\python_d.exe #executable
: C:\Python-2.7.10-64bit-dbg-msvc12\include #includes
: C:\Python-2.7.10-64bit-dbg-msvc12\libs #libs
: <python-debugging>on ;
为了构建 Boost 的调试版本,请先运行 bootstrap.bat
,然后使用以下选项运行b2
:
b2 ^
--build-dir=build__64bit-dbg-msvc12 ^
--build-type=complete ^
--stagedir=stage__64bit-dbg-msvc12 ^
--user-config=my_config.bjam ^
address-model=64 ^
python-debugging=on ^
define=BOOST_PYTHON_DEBUG ^
define=BOOST_PYTHON_NO_LIB ^
link=shared ^
toolset=msvc-12.0 ^
variant=debug ^
stage
这应该可以解决问题。在编译程序的调试版本时,还应定义BOOST_PYTHON_DEBUG
。