使用boost :: Python的:对象会导致连接错误 [英] Using Boost::Python::Object causes linker errors

查看:652
本文介绍了使用boost :: Python的:对象会导致连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我试图把Python到C ++。到目前为止,我已经变得相当,并且已经能够做这样的Python运行琴弦基本的东西。当我试着使用升压:: Python的:对象我开始得到这些连接器4的错误。

我建立使用的bjam与升压1.54.0和Python 2.7.5提升。

的Python库构建命令:

 引导
\\ B2工具= MSVC-10.0 --with-蟒蛇

最少code示例:

 的#include<升压/ python.hpp>
#包括LT&;&iostream的GT;INT主(INT,CHAR **)
{
    Py_Initialize();
    PyRun_SimpleString(进口单位);    提高::蟒蛇::对象main_module =提振::蟒蛇::进口(__ main__);
    提高::蟒蛇::对象main_namespace = main_module.attr(__ dict__);    Py_Finalize();    的std :: cin.get();
    返回0;
}

链接器错误:

  1 GT; PythonTest.obj:错误LNK2019:​​无法解析的外部符号__declspec(dllimport的)类boost ::蟒蛇:: API ::对象__cdecl提振::蟒蛇::进口(类提高::蟒蛇:: STR)(__imp_?进口@ @蟒提升@@ YA?AVobject @ API @ 12 @ VSTR @ 12 @@ Z)函数_main引用
1> PythonTest.obj:错误LNK2019:​​无法解析的外部符号__declspec(dllimport的)保护:__thiscall提振::蟒蛇::详细:: str_base :: str_base(字符常量*)(__imp _ ?? 0str_base @ @细节@蟒提振@@ IAE @ PBD @ Z)的函数引用公众:__thiscall提振::蟒蛇::海峡:: STR(字符常量*)(?? 0str @蟒蛇@提振@@ QAE @ PBD @ Z)
1> E:\\开发\\ PythonTest \\调试\\ PythonTest.exe:致命错误LNK1120:2无法解析的外部


解决方案

Boost.Python的可内置静态或动态链接。这是由 BOOST_PYTHON_STATIC_LIB控被定义或在生成过程中没有定义。该 boost邮件列表似乎以指示此确定指标和内​​置类型是一些混乱的结果。

BOOST_PYTHON_STATIC_LIB 没有定义,那么假定Boost.Python的动态链接。其结果是,Boost.Python的装饰为符号可见为dllimport和DLLEXPORT 。联动期间基于链接器的错误,这个例子code期待导入的boost ::蟒蛇::进口()功能。如果您已经验证Boost.Python库,被链接,那么问题很可能对静态链接,这里的功能没有装饰的出口正在修建的Boost.Python的结果。要解决此问题,请执行下列操作之一:


  • 构建Boost.Python的动态链接(即确保 BOOST_PYTHON_STATIC_LIB 没有定义)。

  • 定义 BOOST_PYTHON_STATIC_LIB 构建示例code时。

  • 定义 BOOST_PYTHON_STATIC_LIB 在这个例子code的的包括升压/ python.hpp

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 <boost/python.hpp>
#include <iostream>

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.

这篇关于使用boost :: Python的:对象会导致连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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