Boost Python Hello World示例不适用于Python
发布时间:2020-12-16 04:57:02 所属栏目:百科 来源:网络整理
导读:我在使用 Python中的Visual C代码(由boost包装)时遇到了很多麻烦. 好吧,我正在使用的工具是:Visual Studio 2010,BoostPro 1_47,Windows 7和Python 2.7(32位). 我有以下代码在Visual Studio 2010中很好地编译: #define BOOST_PYTHON_STATIC_LIB#include boo
我在使用
Python中的Visual C代码(由boost包装)时遇到了很多麻烦.
好吧,我正在使用的工具是: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 Filesboostboost_1_47;C:Python27include . >>> LIBRARY DIRECTORIES: C:Program Filesboostboost_1_47lib;C:Python27libs 所有这些都使得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文件.我是否必须更改文件的位置?如果是的话,我应该把它放在哪里? 非常感谢您的帮助 解决方法
AFAIK你必须将DLL的扩展名更改为.pyd,否则Python将无法加载它.我想你可以设置一个构建选项来自动设置VS中的扩展,但我不确定.
另外,确保创建的扩展名在 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |