boost_python你好示例不起作用

boost_python hello example not working

本文关键字:不起作用 你好 python boost      更新时间:2023-10-16

我正在尝试获取用于提升 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

当我尝试通过执行导入hello.so将其导入python时,出现以下错误:

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

有什么想法吗?

事实证明,

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

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