简体   繁体   English

我需要为Boost.Python定义一个PyMODINIT_FUNC来公开一个C ++类吗?

[英]do I need to define an PyMODINIT_FUNC for Boost.Python to expose a C++ class?

I have libboost installed via Ubuntu. 我已经通过Ubuntu安装了libboost。 The boost version is 1.42. Boost版本为1.42。 I've followed the example on the Boost website: 我在Boost网站上遵循了以下示例:

#include <string>

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

then created the idl: 然后创建idl:

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

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

and build it with bjam: 并用bjam构建它:

using python ;

lib libboost_python : : <name>boost_python ;

project
    : requirements <library>libboost_python
;

python-extension world : world.cpp ;

But as soon as I import world, I get: 但是,一旦我导入世界,我就会得到:

Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import world
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initworld)
>>> 

I'm guessing its looking for a function like this: 我猜想它正在寻找这样的功能:

PyMODINIT_FUNC initworld(void) {
...
}

But I isn't Boost.Python supposed create this? 但是我不是Boost.Python应该创建这个吗? Is the function being generated, but just not found? 函数是否正在生成,但找不到? Or do I need to write it myself? 还是我需要自己写?

I know its really attempting to import the generated module, because it gives a different error when executing from another directory. 我知道它确实尝试导入生成的模块,因为从另一个目录执行时会给出不同的错误。

jsnavely@jsnavely-OptiPlex-980:~/Dropbox/flycap/pytest$ bjam
...found 10 targets...
...updating 5 targets...
MkDir1 bin
MkDir1 bin/gcc-4.5.2
MkDir1 bin/gcc-4.5.2/debug
gcc.compile.c++ bin/gcc-4.5.2/debug/world.o
gcc.link.dll bin/gcc-4.5.2/debug/world.so
...updated 5 targets...
jsnavely@jsnavely-OptiPlex-980:~/Dropbox/flycap/pytest$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import world
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named world
>>> 

In the correct directory: 在正确的目录中:

jsnavely@jsnavely-OptiPlex-980:~/Dropbox/flycap/pytest$ cd bin/gcc-4.5.2/debug/
jsnavely@jsnavely-OptiPlex-980:~/Dropbox/flycap/pytest/bin/gcc-4.5.2/debug$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) 
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import world
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initworld)

ok, I found out why this wasn't working. 好的,我发现了为什么这没用。 My C++ class was in one file, and my IDL was in another. 我的C ++类位于一个文件中,而IDL位于另一个文件中。 Because it didn't look like python OR C++ to me, I thought it belonged in separate file. 因为对我来说它看起来不像python或C ++,所以我认为它属于单独的文件。 So the IDL stuff was never getting picked up. 因此,IDL的东西从未被捡起。

Once I put it all together in World.cpp, everything worked as expected! 一旦将所有内容放到World.cpp中,一切都会按预期进行! Very smoothly, too. 也非常顺利。

I figured this out by looking at the Yet Another Python GraphViz Binding project, and saw that all their Boost.Python stuff was shoved into the same file as the class itself. 我通过查看Yet Another Python GraphViz Binding项目了解了这一点,并发现所有Boost.Python东西都被塞入了类本身的同一个文件中。 http://code.google.com/p/yapgvb/ http://code.google.com/p/yapgvb/

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

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