Language adhesive -SWIG

zhaozj2021-02-11  230

What is SWIG?

SWIG, that is, simplified wrapper and interface generator, which is "a software written to various advanced programming languages" written by C, C , and Objective-C). development tools". SWIG is supported by UNIX, Windows, and Macintosh systems.

For Java programmers, SWIG is an ideal tool to embed C / C code into the Java script. SWIG understands the C / C declaration, so it can parse the C / C header file and generate a Java model and encode it. After compiling, you can use it in Java.

SWIG actually acts as an input, but it is composed of standard C / C header definitions with several (optional) indicated. In fact, if the code is quite clean, you can use the header file itself. The code generated is complete (that is, there is no residual thing to make you must fill it), and the code consists of the Java model and C implement file, it only needs to be compiled.

SWIG support language

Swig supported language very wide, almost all popular languages, currently supporting C / C conversion language with Java / C # / PHP / Python / Perl / Perl5 / Ruby / OCAML / TCL / Chicken. Specific use methods for various languages ​​can refer to the usage manual.

The following is a process of integrating C programs using SWIG as an example below. (Specific procedure SWIG manual has a detailed description).

Download and install SWIG

Download the latest SWIG program from the official website or open source project to decompress the local directory.

Install JDK and add JNI package

Download the latest Java JDK and JNI development kits from Sun's official website.

Use VS6.0 for SWIG configuration

l Open a new project and build a DLL project through the wizard.

l Adding an SWIG interface file (generally in .i end, such as eXample.i), and SWIG will be generated (IE.EXAMPLE_WRAP.C). Note If you use C to select a different suffix, you don't have to worry that the package file does not have -vs automatically update.

l Select the SWIG interface file (Example.i), press Right, and select "Settings" option in the pop-up menu.

l Enter "SWIG" in Description.

l In the Commands text box:

Echo in Order To Function Correctly, please ensure The Following Environment Variables Are Correctly Set:

Echo java_include:% java_include%

Echo java_bin:% java_bin%

Echo Swig:% SWIG%

echo on

% SWIG% / SWIG.EXE-C -java $ (InputPath ".

The% SWIG% is the file directory of SWIG.EXE defined in environment variables.

l In the Outputs text box:

$ (InputName) _wrap.cxx, represents the corresponding C package file according to the SWIG interface file (Example.i), where the package file is named: eXample_wrap.cxx.

l The entire project then select the C / C property page, select "Category" PreProcessor item. Enter the directory containing Java and JNI header files in "Addition Include Directories", Java_Include, Java_include / Win32. L and select the link property page and select "Category" item. Set the name of the output DLL file in Out Put file name.

l Finally, in the POST-Build Step Properties page, add commands to compile Java files through Javac through the POST-Build Command (s):% java_bin% / javac * .java.

L The entire setting process has been completed, it is necessary to explain that the generated JNI function interface definition is defined by the SWIG interface file (Example.i) and method name, in the interface file, by module name:% module example, and C The method name is composed. For example, there is a method start () in C , then the JAVA_EXAMPLEJNI_START () described in JNI is in JNI, and if the interface file defines the% module example_test, the function interface definition in the JNI also changes to java_example_testjni_start (). At the same time, the definition of the JNI function interface also requires the package structure of the Java program called.

l Compile operation.

SWIG resources

Official website: http://www.swig.org

Open source project: http://sourceforge.net/projects/swig/

转载请注明原文地址:https://www.9cbs.com/read-4428.html

New Post(0)