SWIG Multi-language Interface transform "turn"

Source: Internet
Author: User
Tags set set

A SWIG is an abbreviation for simple Wrapper and Interface generator, and is a tool to help create APIs for other programming languages using C or C + + software. For example, I want to create a. NET API for a program written in C + +, in general I have to use managed C + + (Managed C + +) to write a lot of code to generate its. NET API. With Swig, the work of this machine will become very simple. You just need to use an interface file to tell Swig that creating. NET Api,swig for those classes will automatically help you build its. NET API.

Of course, swig not only supports the creation of. NET APIs. The latest version of Swig supports common scripting languages Perl, PHP, Python, TCL, Ruby and non-scripting languages C #, Common Lisp (Clisp, Allegro CL, Cffi, Uffi), Java, Modula-3, OCaml, and R , or even a compiler or assembler program application (Guile, Mzscheme, Chicken). This means that the C + + interface can be encapsulated in a form that can be called by a language such as Java Swig.

Two Environment construction

1. Download the Swigwin and unzip it in the E:/lib directory, i.e. install the swig into the E:/lib/swig directory.

2. Create a new Win32 Console application, and note the selection of DLLs and empty projects in application settings. After completion, change the project configuration to release

3. Add to VC + + directory, projects and solutions, tools, options That is, add swig to the VC executable directory.

----------------if encapsulated as a Python-usable API, perform the following steps----------------------------

4. Download the python2.5, install to the D:/program Files directory, and add D:/program files/python2.5 to the environment variable path.

5. Add D:/program Files/python2.5/include to VC's include path, will D:/program Files/python2.5/libs join VC's library path.

-----------------if encapsulated as a Java-available API, perform the following steps--------------------------------

6. Download the JDK, install to the D:/program Files directory, and add D:/program files/java/jdk1.6.0_10/bin to the environment variable path (if Oracle was previously installed, D:/program Files /java/jdk1.6.0_10/bin is placed in front of Oracle/jre/1.3.1/bin, otherwise the Oracle's JDK is selected by default at compile time.

7. Add D:/program Files/java/jdk1.6.0_10/include/win32 and D:/program files/java/jdk1.6.0_10/include to the include path to VC.

Three Interface file

To create an ***.I interface file in a C + + project, tell Swig to create an API for those methods of those classes.

Interface file Annotations:

1. The module name is given by the specified%module (or with the-module command line option). This indicative text must be written in the header of the file and used when used as an extension object (in addition, the module name is often defined in the target language as a namespace to use). If the module name is already given in the command line, the system will not consider the module name indicated by%module.

For Python:module name Specifies the name of the xxx that generated the file xxx.py,

For Java:module name Specifies the name of the xxx that generated the file Xxx.java

2. Everything inside the%{...%} block will be simply copied verbatim as a result into the wrapper (wrapper) file created by Swig. Most of this is used to include header files and other declarations needed to generate wrapper code. It's important to emphasize that because you include a declaration in a swig input file that does not automatically appear in the generated wrapper code, you need to be sure that you actually put the correct header file in the%{.%} section. It should be noted here that Swig does not parse and interpret the text attached to the%{.%} section. The syntax and semantics within Swig's%{...%} are very similar to the Declarations section in the input file.

3. If you intend to create an API for all methods in a class, there is a very simple way to use the%include tag in the class declaration section of the interface file. Swig will parse the file specified by%include, and all public methods in the class will be exposed in the API.

/* SWIGTEST.I */

%module Swigtest

%{

#include "SwigTest.h"

%}

%include "SwigTest.h"//Don't mix with #

--------------------------Swig Library module accesses some of the standard C + + libraries including STL methods-------------------

Swig support for some language modules makes it a more comprehensive but rarely supported library.
The following is a table that represents the C + + class and supported C + + libraries and Swig interface files
C + + Class C + + library file SWIG Interface library file
std::d eque deque std_deque.i
Std::list List std_list.i
Std::map Map STD_MAP.I
std::p Air Utility STD_PAIR.I
Std::set Set STD_SET.I
std::string string std_string.i
Std::vector Vector std_vector.i

As a result, when using these libraries in C + + code, you can add the swig corresponding interface file to your own interface

Components. such as:%include "STD_STRING.I",

The library file fully recognizes the C + + namespace. If you output std::string or rename it to another type. Make sure you include this rename declaration in your interface file. For example:
%module Example
%include "STD_STRING.I"

using namespace Std;
typedef std::string string;
...
void foo (string s, const string &t); Std_string Typemaps still applied

When encapsulating Java-called APIs and passing parameters that contain Chinese, because string in C + + uses single-byte encoding, whereas string in Java uses Unicode encoding, in order to pass without garbled characters, you can include%include "std_ WSTRING.I "Because Wstring uses the wchar_t type, which is a wide character that satisfies the requirements of non-ASCII characters, such as Unicode encoding.

When encapsulating the API called by Python and passing parameters that contain Chinese, you do not have to consider this coding problem, you can directly use string in C + +.

Four Compiling the module

1. Write Interface Execution command

After having the interface file to compile the interface file, right-click on the interface file, modify its properties, use the Custom build tool, command line content is swig.exe-c++-python swigtest.i , the output (Outputs) is $ (inputname) _wrapper.cpp;

echo Java_include:%java_include%

echo Java_bin:%java_bin%

echo on

Swig.exe-c++-python swigtest.i

command-line parameter description:

① in order to compile JAVA or PYTHON modules, you must include their include and BIN directories, and you can set Java_include, Java_bin, Python_include, Python_bin, and so on in path. The include can be set in the project properties, if it is already set in the project properties, there is no need to include the first two sentences here.

② "Swig.exe" means to invoke the executable file of the swig you installed, before it has been added to the VC executable directory, so here you can no longer write the path, or to find the Swig path.

③ "-c++" means that you want to encapsulate C + + code (not write the default is encapsulate C code),

④ "-python" means to encapsulate the Python interface (Swig can also be encapsulated as Java, Ruby, etc.),

⑤ "$ (InputName) _wrapper.cpp" indicates the name of the C + + code file to be generated.

There are other SWIG commands, such as the –package command, which can be used as:

Swig-java-package Com.swig-outdir Com/swig example.i

When generating the Java API, this command causes the generated Java file to be included in a package.

2. Compile (right-click interface file, compile)

For the APIs available to build Python: Executing the above command generates two new files, one for swigtest.py and one for Swigtest_wrapper.cpp.

For Java-generated APIs: Executing the above command generates a Java class file: Swigtestjni.java,swigtest.java

and C file swigtest_wrapper.cpp.

3. Adding Xxx_wrapper.cpp to the project

4. Modify Project Properties:

-----------------------encapsulates the APIs available in Java---------------------

Linkeràgeneralàoutput file changed to SwigTest.dll

Build Eventsàpost-build Eventàcommand Line changed to:

echo on

"%java_bin%/javac" *.java

This command works by calling Javac after compilation to compile all. java files under the current compilation path into a. class binary file.

-----------------------encapsulates the available APIs for Python---------------------

Linkeràgeneralàoutput file to _swigtest.pyd (note must be underlined)

There are some configurations that are default in VC2005, but not in VC2003, please be aware, including

①c/c++àcode Generationàruntime Library to select the multi-threaded DLL (/MD)

②c/c++àlanguageàtreat wchar_t as built-in type to select Yes (if the conversion does not involve WCHAR and so on, you do not need to handle this)

③c/c++àlanguageàenable run-time Type info to select Yes

5. Build Project

Five The generated APIs can be used in the corresponding scripting language (Java, Python, and so on).

Windows environment version 2.0.7: Http://prdownloads.sourceforge.net/swig/swigwin-2.0.7.zip

SWIG Multi-language Interface transform "turn"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.