【问题标题】:boost_python hello example not workingboost_python hello 示例不起作用
【发布时间】:2015-01-30 14:13:51
【问题描述】:

我正在尝试让 boost python 工作的 hello world 示例。我正在使用OSXboost 1.55python 2.7

这是我的hello.cpp

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>

char const* greet()
{
   return "hello, world";
}

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

我用以下两行编译它:

g++ -fPIC -I/usr/include/python2.7/ -I/usr/local/include/ -c hello.cpp
g++ -shared -Wl, -o hello.so hello.o -L/usr/lib -L/usr/local/lib -lpython2.7 -lboost_python

当我尝试通过 import hello.so 将其导入 python 时,我收到以下错误:

ImportError: dynamic module does not define init function (inithello)

有什么想法吗?

【问题讨论】:

    标签: python c++ boost boost-python


    【解决方案1】:

    原来BOOST_PYTHON_MODULE 中的名称必须与库的名称匹配,所以我将链接步骤更改为

    g++ -shared -Wl, -o hello_ext.so hello.o -L/usr/lib -L/usr/local/lib -lpython2.7 -lboost_python
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2012-06-17
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      相关资源
      最近更新 更多