2012-11-17 37 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