如何从 Visual Studio 2010 使用 SWIG 创建 DLL

How to create a DLL with SWIG from Visual Studio 2010(如何从 Visual Studio 2010 使用 SWIG 创建 DLL)

本文介绍了如何从 Visual Studio 2010 使用 SWIG 创建 DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数周以来,我一直在尝试让 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.

  1. 启动 Visual Studio 2010
  2. 从现有代码文件、新建、项目...
  3. 选择Visual C++"项目类型,然后单击下一步".
  4. 输入 .cpp/.h/.i 文件所在的项目文件位置.
  5. 对于项目名称,选择 .i 文件中 %module 语句中使用的名称(大小写重要).
  6. 选择项目类型动态链接库 (DLL) 项目",然后单击下一步.
  7. 将 Python.h 文件的路径添加到包含搜索路径,通常类似于C:Python27include",然后单击下一步.
  8. 点击完成.
  9. 右键单击解决方案资源管理器中的项目、添加、现有项目...,然后选择您的 .i 文件.
  10. 右键单击 .i 文件、属性,然后选择配置所有配置".
  11. 将项目类型更改为自定义构建工具",然后单击应用.
  12. 在属性中选择自定义构建工具"(它会在上面应用后出现).
  13. 输入swig -c++ -python -outdir $(Outdir) %(Identity)"的命令行(假设 SWIG 在您的路径中,并根据需要将生成的 .py 文件重定向到 Debug 或 Release 目录).
  14. 在输出中输入%(Filename)_wrap.cxx;$(Outdir)%(Filename).py".
  15. 点击确定.
  16. 右键单击 .i 文件,然后选择编译.
  17. 右键单击项目,添加、新建过滤器,将其命名为生成的文件".
  18. 右键单击生成的文件",单击属性",然后将SCC 文件"设置为假"(如果您使用源代码控制,这会阻止 VS2010 尝试在此过滤器中检入生成的文件).
  19. 右键单击生成的文件",添加,退出项目,然后选择编译生成的 _wrap.cxx 文件.
  20. 右键单击项目、属性.
  21. 选择配置所有配置".
  22. 选择 Configuration Properties、Linker、General、Additional Library Directories 并添加 python 库的路径,通常是C:Python27libs".
  23. 选择 Configuration Properties、General 并将 TargetName 设置为_$(ProjectName)".
  24. 将目标扩展名设置为.pyd".
  25. 构建项目的发布"版本.除非您构建 Python 本身的调试版本,否则您无法构建调试版本.
  26. 打开一个控制台,进入项目的 Release 目录,运行 python,导入你的模块,并调用一个函数!

这篇关于如何从 Visual Studio 2010 使用 SWIG 创建 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何从 Visual Studio 2010 使用 SWIG 创建 DLL

基础教程推荐