2012-11-17 38 views
5

嘿家伙我很新,以提高C/C++库。我下载了boost库并构建了库。 我使用boost接口在C++中创建了一个非常简单的python库(实际上它是文档中给出的示例代码)。我把它建成一个dll文件。在文档中,它读取这个dll暴露给python,他们只是在python中显示导入功能,并包括创建的库。 我不明白如何将该dll暴露给python并以传统('import')方式加载库。 在情况下,如果你想看看代码,然后在这里它是:Python扩展建设与提升

#include <boost/python.hpp> 

char const* greet() 
{ 
    return "hello, world"; 
} 

BOOST_PYTHON_MODULE(hello_ext) 
{ 
    using namespace boost::python; 
    def("greet", greet); 
} 

请帮助我真的想建立与C/C++和Python应用程序。 我只是想用hello_ext为:

>>>import hello_ext 
>>>print hello_ext.greet() 

谢谢。

+0

你的编译生成一个dll文件吗?你有错误吗?当你运行'import hello_ext'时会发生什么? – Xymostech

+0

是的,我的编译确实产生了一个DLL,当我导入hello_ext时,解释器引发了ImportError:没有名为hello_ext的模块。 – Xk0nSid

回答

1

I built it into a dll file. In the documentation it reads that this dll is exposed to python and they just show the import function in python and include the created library. I don't understand how to expose that dll to python and load the library inside in tradition ('import') manner.

您需要将该共享库放入the module search path。有几种方法可以实现这一点。

之一是:

import sys 
sys.path.append("<directory-where-hello_ext-module-resides>") 
import hello_ext 

你的共享库应该叫hello_ext.dll

+0

该共享库(Dll)位于python的DLL文件夹中。但我仍然尝试并从解释器中添加该目录,但它仍然表示该模块不存在。无论如何,谢谢你的回复。 – Xk0nSid

+0

@ user1544053你的DLL的确切名称是什么? –

+0

它是PythonDll.dll,它驻留在“Python27/DLLs/PythonDll.dll”中。 – Xk0nSid