简体   繁体   English

boost_python你好示例不起作用

[英]boost_python hello example not working

I am trying to get the hello world example for boost python working. 我正在尝试获取有关Boost python工作的世界示例。 I am using OSX , boost 1.55 and python 2.7 我正在使用OSXboost 1.55python 2.7

Here is my hello.cpp 这是我的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);
}

and I have compiled it with the following two lines: 我用以下两行代码对其进行了编译:

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

When I try to import it into python by doing import hello.so I get the following error: 当我尝试通过导入hello.so将其导入python时,出现以下错误:

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

Any ideas? 有任何想法吗?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM