Boost Python类导出无法通过Visual Studio 2013中的链接错误进行编译 [英] Boost Python class export fails to compile with linking error in visual studio 2013

查看:112
本文介绍了Boost Python类导出无法通过Visual Studio 2013中的链接错误进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编译了Boost自己,并用它将以下函数导出到DLL中:

I compiled Boost myself and used it to export the following function to a DLL :

#include <boost/python.hpp>
using namespace boost::python;

std::string greet()
{
    return "hello, dude !!";
}

BOOST_PYTHON_MODULE(hello)
{
    def("greet", greet);
}

在我将hello.dll文件重命名为hello.pyd之后,此方法在Python中运行良好.

This loaded fine in Python after I renamed the hello.dll file to hello.pyd.

现在我正在尝试:

#include <boost/python.hpp>
using namespace boost::python;

struct World
{
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};


BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World")
        .def("greet", &World::greet)
        .def("set", &World::set);
}

出现错误:

Error   29  error LNK2019: unresolved external symbol "__declspec(dllimport) void * __cdecl boost::python::objects::find_static_type(void *,struct boost::python::type_info,struct boost::python::type_info)" (__imp_?find_static_type@objects@python@boost@@YAPAXPAXUtype_info@23@1@Z) referenced in function "private: virtual void * __thiscall boost::python::objects::value_holder<struct World>::holds(struct boost::python::type_info,bool)" (?holds@?$value_holder@UWorld@@@objects@python@boost@@EAEPAXUtype_info@34@_N@Z) D:\Code\Python\hello\hello\hello.obj    hello

Error   30  error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall boost::python::converter::shared_ptr_deleter::shared_ptr_deleter(struct boost::python::converter::shared_ptr_deleter const &)" (__imp_??0shared_ptr_deleter@converter@python@boost@@QAE@ABU0123@@Z) referenced in function "public: __thiscall boost::shared_ptr<void>::shared_ptr<void><void,struct boost::python::converter::shared_ptr_deleter>(void *,struct boost::python::converter::shared_ptr_deleter)" (??$?0XUshared_ptr_deleter@converter@python@boost@@@?$shared_ptr@X@boost@@QAE@PAXUshared_ptr_deleter@converter@python@1@@Z) D:\Code\Python\hello\hello\hello.obj    hello

Error   31  error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall boost::python::objects::class_base::~class_base(void)" (__imp_??1class_base@objects@python@boost@@QAE@XZ) referenced in function __unwindfunclet$??0?$class_@UWorld@@Unot_specified@detail@python@boost@@U2345@U2345@@python@boost@@QAE@PBD0@Z$0    D:\Code\Python\hello\hello\hello.obj    hello

Error   32  error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl boost::python::objects::register_dynamic_id_aux(struct boost::python::type_info,struct std::pair<void *,struct boost::python::type_info> (__cdecl*)(void *))" (__imp_?register_dynamic_id_aux@objects@python@boost@@YAXUtype_info@23@P6A?AU?$pair@PAXUtype_info@python@boost@@@std@@PAX@Z@Z) referenced in function "void __cdecl boost::python::objects::register_dynamic_id<struct World>(struct World *)" (??$register_dynamic_id@UWorld@@@objects@python@boost@@YAXPAUWorld@@@Z)  D:\Code\Python\hello\hello\hello.obj    hello

这是Visual Studio Express2013.我按照此处给出的确切步骤编译了自己的boost库:

This is Visual Studio Express 2013. I compiled my own boost libraries following the exact steps as given here: http://www.boost.org/doc/libs/1_56_0/doc/html/bbv2/installation.html

我认为我的构建/安装很好,因为在此之前可以使用全局功能.但是也许与MSVC 2012和13不兼容,因为boost python调试库的名称为boost_python-vc120-mt-gd-1_56.lib.

I think my build/installation is fine as the global function before this was fine. But maybe there is some incompatibility with MSVC 2012 and 13 because the boost python debug library is named boost_python-vc120-mt-gd-1_56.lib.

推荐答案

我的答案是: 使用Boost :: Python :: Object会导致链接器错误

当bjam使用以下步骤给出的步骤编译增强的静态链接时:

As bjam compiles boost for static linking using the steps given at : http://www.boost.org/doc/libs/1_56_0/doc/html/bbv2/installation.html

我需要定义BOOST_PYTHON_STATIC_LIB,否则它将假定为Boost.python库进行动态链接.

I need to define BOOST_PYTHON_STATIC_LIB or it will assume dynamic linkage for the Boost.python library.

编译代码:

#define BOOST_PYTHON_STATIC_LIB    
#include <boost/python.hpp>
using namespace boost::python;


struct World
{
    void set(std::string msg) { this->msg = msg; }
    std::string greet() { return msg; }
    std::string msg;
};


BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World")
        .def("greet", &World::greet)
        .def("set", &World::set);
}

使用hello.dll的一种方法是,首先将hello.dll重命名为hello.pyd,然后将其复制到Python.exe的起始目录,然后:

One way to use hello.dll, is to first rename hello.dll to hello.pyd and copy it to the directory where Python.exe starts from, then :

Python代码:

import hello
obj = hello.World()
obj.set("Hello World!")
obj.greet()

这篇关于Boost Python类导出无法通过Visual Studio 2013中的链接错误进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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