問題描述
數周以來,我一直在嘗試讓 Microsoft Visual Studio 2010 使用 SWIG 為我創建一個 DLL.如果你已經經歷過這個過程,你會不會好心給一個周到的一步一步的過程解釋?我在網上到處找,并花了很多時間試圖做到這一點;但我發現的所有教程都已過時或解釋不當.
I've been trying for weeks to get Microsoft Visual Studio 2010 to create a DLL for me with SWIG. If you have already gone through this process, would you be so kind as to give a thoughtful step-by-step process explanation? I've looked everywhere online and have spent many many hours trying to do this; but all of the tutorials that I have found are outdated or badly explained.
我已經成功地使用 cygwin 完成了這個過程;但正如你們中的一些人所知,cygwin DLL 不是很實用.
I have succeeded in going through this process with cygwin; but as some of you know, a cygwin DLL is not very practical.
因此,我知道可以一起創建 DLL 的 .i、.cpp 和 .h 文件.我只需要知道如何使用 Visual Studio C++ 2010 執行此操作.我的目標語言是 Python.
As a result, I have .i, .cpp, and .h files that I know can create a DLL together. I just need to know how to do this with Visual Studio C++ 2010. The language that I am targeting is Python.
推薦答案
分步說明.這假設您擁有源代碼并且正在構建一個將源直接鏈接到它的 DLL 擴展.在創建了一個工作項目后,我沒有回頭,所以我可能錯過了一些東西.如果您卡在某個步驟上,請評論此帖子.如果您有一個現有的 DLL 并且想要創建一個包裝它的 Python 擴展 DLL,則此步驟略有不同.如果你在這篇文章的評論方面需要幫助,我會擴展它.
Step-by-step instructions. This assumes you have the source and are building a single DLL extension that links the source directly into it. I didn't go back through it after creating a working project, so I may have missed something. Comment on this post if you get stuck on a step. If you have an existing DLL and want to create a Python extension DLL that wraps it, this steps are slightly different. If you need help with that comment on this post and I will extend it.
Edit 8/19/2012:如果從 C 示例開始,請不要在步驟 13 中使用 -c++
并使用 .c
> 代替 .cxx
用于步驟 14 和 19 中的包裝文件擴展名.
Edit 8/19/2012: If starting with a C example, don't use -c++
in step 13 and use .c
instead of .cxx
for the wrap file extension in steps 14 and 19.
- 啟動 Visual Studio 2010
- 從現有代碼文件、新建、項目...
- 選擇Visual C++"項目類型,然后單擊下一步".
- 輸入 .cpp/.h/.i 文件所在的項目文件位置.
- 對于項目名稱,選擇 .i 文件中 %module 語句中使用的名稱(大小寫重要).
- 選擇項目類型動態鏈接庫 (DLL) 項目",然后單擊下一步.
- 將 Python.h 文件的路徑添加到包含搜索路徑,通常類似于C:Python27include",然后單擊下一步.
- 點擊完成.
- 右鍵單擊解決方案資源管理器中的項目、添加、現有項目...,然后選擇您的 .i 文件.
- 右鍵單擊 .i 文件、屬性,然后選擇配置所有配置".
- 將項目類型更改為自定義構建工具",然后單擊應用.
- 在屬性中選擇自定義構建工具"(它會在上面應用后出現).
- 輸入swig -c++ -python -outdir $(Outdir) %(Identity)"的命令行(假設 SWIG 在您的路徑中,并根據需要將生成的 .py 文件重定向到 Debug 或 Release 目錄).
- 在輸出中輸入%(Filename)_wrap.cxx;$(Outdir)%(Filename).py".
- 點擊確定.
- 右鍵單擊 .i 文件,然后選擇編譯.
- 右鍵單擊項目,添加、新建過濾器,將其命名為生成的文件".
- 右鍵單擊生成的文件",單擊屬性",然后將SCC 文件"設置為假"(如果您使用源代碼控制,這會阻止 VS2010 嘗試在此過濾器中檢入生成的文件).
- 右鍵單擊生成的文件",添加,退出項目,然后選擇編譯生成的 _wrap.cxx 文件.
- 右鍵單擊項目、屬性.
- 選擇配置所有配置".
- 選擇 Configuration Properties、Linker、General、Additional Library Directories 并添加 python 庫的路徑,通常是C:Python27libs".
- 選擇 Configuration Properties、General 并將 TargetName 設置為_$(ProjectName)".
- 將目標擴展名設置為.pyd".
- 構建項目的發布"版本.除非您構建 Python 本身的調試版本,否則您無法構建調試版本.
- 打開一個控制臺,進入項目的 Release 目錄,運行 python,導入你的模塊,并調用一個函數!
這篇關于如何從 Visual Studio 2010 使用 SWIG 創建 DLL的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!