【问题标题】:Boost Python class export fails to compile with linking error in visual studio 2013Boost Python 类导出无法编译并在 Visual Studio 2013 中出现链接错误
【发布时间】:2014-10-14 14:08:27
【问题描述】:

我自己编译了 Boost 并用它来将以下函数导出到 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 中加载良好。

现在我正在尝试这个:

#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 Express 2013。我按照此处给出的确切步骤编译了自己的 boost 库: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

【问题讨论】:

    标签: python c++ boost boost-python


    【解决方案1】:

    我得到了答案: Using Boost::Python::Object causes linker errors

    当 bjam 使用以下给出的步骤为静态链接编译 boost 时:http://www.boost.org/doc/libs/1_56_0/doc/html/bbv2/installation.html

    我需要定义 BOOST_PYTHON_STATIC_LIB 否则它将假定 Boost.python 库的动态链接。

    编译代码:

    #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 启动的目录,然后:

    Python 代码:

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

    【讨论】:

      猜你喜欢
      • 2014-09-06
      • 2016-12-20
      • 2021-04-30
      • 2012-02-01
      • 1970-01-01
      • 2016-04-09
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      相关资源
      最近更新 更多