2012-06-14 109 views
7

我用我的C++代码从Python中的Visual C++(由升压包裹),具有带来很大的麻烦不工作。升压Python的Hello World示例在Python

好了,所以我使用的工具有: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); 
} 

它的格式为: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 

所有这一切都使得C++文件构建但我不能用Python访问它。

这是Python的说什么,当我尝试使用模块:

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

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

当C++代码编译它创建一个DLL文件。我必须更改文件的位置吗?如果是这样,我应该把它放在哪里?

您的帮助将不胜感激

回答

11

据我所知,你必须将DLL的扩展名更改为.pyd或以其他方式Python将无法加载它。我认为你可以设置一个构建选项来自动在VS中设置扩展名,但我不确定。

此外,还要确保创建扩展某处PYTHONPATH,道路,蟒蛇会寻找模块加载。

+0

谢谢Constantinius! :D我终于得到这个工作,几小时,几小时后,试图让boost.python做它的事情。 – user1449530

+0

你能提供一个你已经做了成功的秘诀吗? – Noam

+0

我还必须将包含Boost Python * .lib和* .dll文件的目录添加到PATH –