2 引言

2.1 SWIG 是什么?

SWIG is a software development tool that simplifies the task of interfacing different languages to C and C++ programs. In a nutshell, SWIG is a compiler that takes C/C++ declarations and creates the wrappers needed to access those declarations from other languages including Perl, Python, Tcl, Ruby, Guile, and Java. SWIG normally requires no modifications to existing code and can often be used to build a usable interface in only a few minutes. Possible applications of SWIG include:

  • Building interpreted interfaces to existing C programs.
  • Rapid prototyping and application development.
  • Interactive debugging.
  • Reengineering or refactoring of legacy software into scripting language components.
  • Making a graphical user interface (using Tk for example).
  • Testing of C libraries and programs (using scripts).
  • Building high performance C modules for scripting languages.
  • Making C programming more enjoyable (or tolerable depending on your point of view).
  • Impressing your friends.
  • Obtaining vast sums of research funding (although obviously not applicable to the author).

SWIG was originally designed to make it extremely easy for scientists and engineers to build extensible scientific software without having to get a degree in software engineering. Because of this, the use of SWIG tends to be somewhat informal and ad-hoc (e.g., SWIG does not require users to provide formal interface specifications as you would find in a dedicated IDL compiler). Although this style of development isn't appropriate for every project, it is particularly well suited to software development in the small; especially the research and development work that is commonly found in scientific and engineering projects. However, nowadays SWIG is known to be used in many large open source and commercial projects.

SWIG 是一款软件开发工具,它简化了将不同语言连接到 C 和 C++ 程序的任务。简而言之,SWIG 是一个编译器,它接受 C/C++ 声明并创建从其他语言(包括 Perl、Python、Tcl、Ruby、Guile 和 Java)访问这些声明所需的包装器。SWIG 通常不需要修改现有代码,并且可以在几分钟内构建可用的接口。SWIG 可能的应用包括:

  • 构建现有 C 程序的解释接口。
  • 快速原型设计和应用程序开发。
  • 交互式调试。
  • 将历史遗留软件重新设计或重构为脚本语言组件。
  • 制作图形用户(例如使用 Tk)。
  • 测试 C 代码库和程序(使用脚本)。
  • 为脚本语言构建高性能 C 模块。
  • 使 C 编程更愉快(或视你的情况而定)。
  • 给你的朋友留下深刻印象
  • 获得大量研究经费(虽然显然不适用于作者本人)。

SWIG 最初旨在使科学家和工程师能够非常轻松地构建可扩展的科学软件,而无需软件工程学位。因此,SWIG 的使用往往是非正式的和临时的(例如,SWIG 不要求用户提供正式的接口规范,就像你在专用的 IDL 编译器中找到的那样)。虽然这种开发方式并不适合每个项目,但它特别适合于小型软件开发,特别是科学和工程项目中常见的研究和开发工作。但是,如今已知 SWIG 可用于许多大型开源和商业项目。

2.2 为什么使用 SWIG?

As stated in the previous section, the primary purpose of SWIG is to simplify the task of integrating C/C++ with other programming languages. However, why would anyone want to do that? To answer that question, it is useful to list a few strengths of C/C++ programming:

  • Excellent support for writing programming libraries.
  • High performance (number crunching, data processing, graphics, etc.).
  • Systems programming and systems integration.
  • Large user community and software base.

Next, let's list a few problems with C/C++ programming

  • Writing a user interface is rather painful (i.e., consider programming with MFC, X11, GTK, or any number of other libraries).
  • Testing is time consuming (the compile/debug cycle).
  • Not easy to reconfigure or customize without recompilation.
  • Modularization can be tricky.
  • Security concerns (buffer overflows for instance).

To address these limitations, many programmers have arrived at the conclusion that it is much easier to use different programming languages for different tasks. For instance, writing a graphical user interface may be significantly easier in a scripting language like Python or Tcl (consider the reasons why millions of programmers have used languages like Visual Basic if you need more proof). An interactive interpreter might also serve as a useful debugging and testing tool. Other languages like Java might greatly simplify the task of writing distributed computing software. The key point is that different programming languages offer different strengths and weaknesses. Moreover, it is extremely unlikely that any programming is ever going to be perfect. Therefore, by combining languages together, you can utilize the best features of each language and greatly simplify certain aspects of software development.

From the standpoint of C/C++, a lot of people use SWIG because they want to break out of the traditional monolithic C programming model which usually results in programs that resemble this:

  • A collection of functions and variables that do something useful.
  • A main() program that starts everything.
  • A horrible collection of hacks that form some kind of user interface (but which no-one really wants to touch).

Instead of going down that route, incorporating C/C++ into a higher level language often results in a more modular design, less code, better flexibility, and increased programmer productivity.

SWIG tries to make the problem of C/C++ integration as painless as possible. This allows you to focus on the underlying C program and using the high-level language interface, but not the tedious and complex chore of making the two languages talk to each other. At the same time, SWIG recognizes that all applications are different. Therefore, it provides a wide variety of customization features that let you change almost every aspect of the language bindings. This is the main reason why SWIG has such a large user manual

最新文章

  1. JSON and Microsoft Technologies(翻译)
  2. osx下配置Cocos2d-x 3.x Android开发环境
  3. [转]uses-permission权限列表
  4. Bulb Switcher
  5. uva146 ID码
  6. iOS开发之修改动画对象的元素属性
  7. Java 集合深入理解(4):List<E> 接口
  8. POJ 1502 MPI Maelstrom
  9. ZOJ 1101 Gamblers
  10. 【CSS系列-选择器优先级总结】
  11. [iOS基础控件 - 6.12.3] @property属性 strong weak copy
  12. JQuery图片滑动插件
  13. Microsoft SQL Server 获得本地帮助方法
  14. python基础知识六
  15. line-height系列——定义和工作原理总结
  16. css左侧固定宽度,右侧自适应的几种实现方法
  17. 学习爬虫的day02 (用线程去爬虫 提高速度)
  18. eclipse使用javaFX写一个HelloWorkld
  19. Tips on GORM, Avoid Error about "duplicate column name: id"
  20. Python开发【内置模块篇】configparser

热门文章

  1. springboot的pom.xml配置
  2. 如何让SQL语句不执行默认排序,而是按照in语句的顺序返回结果
  3. Linux关机重启注销(3)
  4. 设计模式之(十三)外观模式(Facade)
  5. (转)牛牛牌型判定(五小牛 > 五花牛 > 炸弹 > 银牛 > 牛牛 > 有牛>没牛)
  6. android studio学习---签名打包的两种方式
  7. 0x02 Python logging模块利用配置加载logger
  8. Linux shell 函数应用示例01
  9. 设置进程的cpu亲和性
  10. 解决postMessage跨域问题