c++ - Windows/MSVC10 下的 Boost.Python 链接错误

标签 c++ python boost boost-python

我使用 b2 和正确的 Python 配置编译了 boost 1.50.0 库。以下是命令 b2 --debug-configuration 的相关输出:

notice: [python-cfg] Configuring python...
notice: [python-cfg]   user-specified cmd-or-prefix: "C:\Python33z\python"
notice: [python-cfg] Checking interpreter command "C:\Python33z\python"...
notice: [python-cfg] running command 'DIR /-C /A:S "C:\Python33z\python.exe" 2>&
1'
notice: [python-cfg] running command '"C:\Python33z\python" -c "from sys import
*; print('version=%d.%d\nplatform=%s\nprefix=%s\nexec_prefix=%s\nexecutable=%s'
% (version_info[0],version_info[1],platform,prefix,exec_prefix,executable))" 2>&
1'    notice: [python-cfg] Configuring python...
notice: [python-cfg]   user-specified cmd-or-prefix: "C:\Python33z\python"
notice: [python-cfg] Checking interpreter command "C:\Python33z\python"...
notice: [python-cfg] running command 'DIR /-C /A:S "C:\Python33z\python.exe" 2>&
1'
notice: [python-cfg] running command '"C:\Python33z\python" -c "from sys import
*; print('version=%d.%d\nplatform=%s\nprefix=%s\nexec_prefix=%s\nexecutable=%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: "C:\Python33z\python"
notice: [python-cfg]   include path: "C:\Python33z\Include"
notice: [python-cfg]   library path: "C:\Python33z\libs"
notice: [python-cfg]   DLL search path: "C:\Python33z"
notice: [msvc-cfg] msvc-10.0 detected, command: 'C:\Program Files (x86)\Microsof
t Visual Studio 10.0\VC\bin\cl.exe'
notice: will use 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\cl.
exe' for msvc, condition <toolset>msvc-10.0

...updating 4 targets...
msvc.archive bin.v2\libs\python\build\msvc-10.0\debug\link-static\threading-mult
i\libboost_python3-vc100-mt-gd-1_50.lib
common.copy stage\lib\libboost_python3-vc100-mt-gd-1_50.lib
bin.v2\libs\python\build\msvc-10.0\debug\link-static\threading-multi\libboost_py
thon3-vc100-mt-gd-1_50.lib
        1 file(s) copied.
msvc.archive bin.v2\libs\python\build\msvc-10.0\release\link-static\threading-mu
lti\libboost_python3-vc100-mt-1_50.lib
common.copy stage\lib\libboost_python3-vc100-mt-1_50.lib
bin.v2\libs\python\build\msvc-10.0\release\link-static\threading-multi\libboost_
python3-vc100-mt-1_50.lib
        1 file(s) copied.
...updated 4 targets...


The Boost C++ Libraries were successfully built!

我已正确设置链接器目录并链接到适当的输出文件(libboost_ python3-vc100-mt-1_50.lib 发布,libboost_python3-vc100-mt-gd-1_50.lib 调试)。我使用的是自编译的 python 发行版,但我也尝试过使用标准发行版,结果没有任何不同。

我在编译时遇到的链接错误是:

error LNK2019: unresolved external symbol "__declspec(dllimport) protected: __thiscall boost::python::detail::str_base::str_base(char const *)" (__imp_??0str_base@detail@python@boost@@IAE@PBD@Z) referenced in function "public: __thiscall boost::python::str::str(char const *)" (??0str@python@boost@@QAE@PBD@Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall boost::python::detail::str_base::~str_base(void)" (__imp_??1str_base@detail@python@boost@@QAE@XZ) referenced in function "public: __thiscall boost::python::str::~str(void)" (??1str@python@boost@@QAE@XZ)
error LNK2019: unresolved external symbol "__declspec(dllimport) class boost::python::api::object __cdecl boost::python::import(class boost::python::str)" (__imp_?import@python@boost@@YA?AVobject@api@12@Vstr@12@@Z) referenced in function _main
fatal error LNK1120: 3 unresolved externals

产生这些错误的测试代码归结为:

using namespace boost::python;
object main_module = import("__main__");

我使用了具有相同构建配置的 Boost.Filesystem,它没有给我带来任何问题。

我希望有人能帮我解决这个问题,我真的很困惑!

更新:我刚刚阅读了一些关于定义 BOOST_PYTHON_STATIC_LIB 的模糊文本;我不确定我是否朝着正确的方向前进(因为 afaik 在构建过程中没有提到它),但无论哪种方式,它都会给我一个更令人困惑的错误:

LINK : fatal error LNK1104: cannot open file 'python27.lib'

我不知道为什么当 python-cfg 输出清楚地显示它找到我的 python33 dist(我的应用程序已经与之链接;python33.lib)时它尝试与该库链接。

最佳答案

第一个问题:事实证明,定义 BOOST_PYTHON_STATIC_LIB 确实是修复 Unresolved external 链接错误的正确方法。与静态库链接时,它似乎是一个必要的定义;这似乎很明显,但这里没有提到:http://www.boost.org/doc/libs/1_50_0/libs/python/doc/building.html .

第二个问题:添加那个定义后,下一个链接错误发生了,因为我在使用正确的 Python 配置重新构建之前没有运行 b2 clean(即,在清理之前没有重新编译任何东西!),这应该是显而易见的,因为缺少与编译器相关的输出,但它确实愚弄了我,所以这只是另一件需要注意的小事。

关于c++ - Windows/MSVC10 下的 Boost.Python 链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11812463/

相关文章:

c++ - CppUnit 可以用于嵌入式系统吗?

python - 一次打开多个(未指定数量)文件并确保它们正确关闭

python - 错误消息 : replace with Series. 滚动(窗口=5).corr(其他=<系列>)

C++ 单个字符未通过套接字发送

c++ - Boost String Replace 不会用字符串替换换行符

c++ - 乘法时的非法间接

c++ - 在常量成员函数内部调用非常量成员函数

c++ - C++标准库命名不一致

python - 将 Pandas 数据框 reshape 为与重复行一样多的列

c++ - 海龟模型 : MOCK_EXPECT fails if mocked class method returns a value