Boost Python Hello World示例不适用于Python

use*_*530 7 c++ python boost boost-python

我在Python中使用Visual C++(由boost包装)的c ++代码时遇到了很多麻烦.

好吧,我正在使用的工具是:Visual Studio 2010,BoostPro 1_47,Windows 7和Python 2.7(32位).

我有以下代码在Visual Studio 2010中很好地编译:

#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);
}
Run Code Online (Sandbox Code Playgroud)

它的格式为:Win32控制台应用程序>>>空项目/ DLL.

在"项目属性"中:

VC++ DIRECTORIES: 
  I added:  
  >>> INCLUDE DIRECTORIES:  C:\Program Files\boost\boost_1_47;C:\Python27\include        .  
  >>> LIBRARY DIRECTORIES:  C:\Program Files\boost\boost_1_47\lib;C:\Python27\libs
Run Code Online (Sandbox Code Playgroud)

所有这些都使c ++文件构建,但后来我无法从Python访问它.

当我尝试使用模块时,这就是Python所说的:

">>> import hello
 Traceback (most recent call last):
   File "<pyshell#0>", line 1, in <module>
     import hello
 ImportError: No module named hello
Run Code Online (Sandbox Code Playgroud)

所以我想我的问题是......我怎样才能让Python找到它?

当c ++代码编译时,它会创建一个DLL文件.我是否必须更改文件的位置?如果是的话,我应该把它放在哪里?

非常感谢您的帮助

Con*_*ius 11

AFAIK你必须改变DLL的扩展名,.pyd否则Python将无法加载它.我想你可以设置一个构建选项来自动设置VS中的扩展名,但我不确定.

另外,确保创建的扩展名PYTHONPATH在路径上的某个位置,python将查找要加载的模块.