boost python错误,使用boost :: Python的:对象会导致连接错误

So, I'm attempting to embed Python into C++. I have gotten fairly far in, and have been able to do basic things like run strings of Python. As soon as I tried to use Boost::Python::Object I began getting these 4 linker errors.

I built boost using BJAM with Boost 1.54.0 and Python 2.7.5.

Python Lib Build Commands:

bootstrap

.\b2 toolset=msvc-10.0 --with-python

Minimal Code Example :

#include

#include

int main(int, char **)

{

Py_Initialize();

PyRun_SimpleString("import Entity");

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

boost::python::object main_namespace = main_module.attr("__dict__");

Py_Finalize();

std::cin.get();

return 0;

}

Linker Errors:

1>PythonTest.obj : 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

1>PythonTest.obj : 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)

1>E:\Dev\PythonTest\Debug\PythonTest.exe : fatal error LNK1120: 2 unresolved externals

解决方案

Boost.Python can be built for static or dynamic linking. This is controlled by BOOST_PYTHON_STATIC_LIB being defined or not defined during the build process. The boost mailing list seems to indicate that this defintion and built-types are the result of some confusion.

When BOOST_PYTHON_STATIC_LIB is not defined, then Boost.Python assumes dynamic linkage. As a result, Boost.Python decorates symbol visibility for dllimport and dllexport. Based on the linker error, the example code was expecting to import the boost::python::import() function during linkage. If you have verified that the Boost.Python library is being linked, then the problem is likely the result of Boost.Python being built for static linkage, where the functions are not decorated for exporting. To resolve this, do one of the following:

Build Boost.Python for dynamic linkage (i.e. make sure BOOST_PYTHON_STATIC_LIB is not defined).

Define BOOST_PYTHON_STATIC_LIB when building the example code.

Define BOOST_PYTHON_STATIC_LIB in the example code before including boost/python.hpp.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值