Source and discussion: http://www.blogjava.net/calvin/archive/2005/10/24/16606.html
Author: Jiangnan Baiyi
According to swig's ideas,C/C ++ is an excellent library-level code language, while Java, C #, and Python are dominant in writing interfaces.Therefore, swig allows you to call a class library written in C/C ++ in a higher-level language. Instead of a simple cross-language call, swig compiles the class library into a host language.ClassSuch a superior citizen.
I was worried that this project was somewhat marginalized. It seems that Ruby and Python had been using it to slide from C/C ++. For the new generation of Python and Ruby, C/C ++ class libraries are fascinating. Even the rich Java language relies on swig to build a Java version for SDL.
Cyt idol said, "it's a good time to make a product: there are a variety of API interfaces that support Java, Python, PHP, Perl, Ruby ...... How to blow ...... "
1. Implementation principle:
For example, in Java, there is a JNI mechanism that can directly call C code, but the requirements for C Code are as follows:
Jniexport void jnicall java_helloworld_displayhelloworld (jnienv * ENV, jobject OBJ)
{
Printf ("Hello world! /N ");
Return;
}
Swig (simplified wrapper and interface generator) automatically generates the lengthy wrapper file and corresponding Java class for you, and the type conversion is also completed properly, function pointers, templates, and other functions of C ++ can be converted in other languages.
2. Procedure:
It is very simple, especially in VC, the DSP file has been written. Set the option to release and rebuild it.
If you want to split it into slow actions, this is the case in a Java project:
1. Compile the interface file example. I
If the API has header files, the following code defines an example class.
% Module example
% {
# Include "example. H"
%}
% Include "example. H"
2. swig-Java example. I
Generate a c file conforming to the JNI syntax (see above): example_wrap.c
Generate a Java file: examplejni. Java, example. Java
3. VC packages example_wrap.c and example. c into DLL.
4. Use example. Java in a common Java program
System. loadlibrary ("example ");
Int I = new example (). Add (42,105 );
3. Summary
When you are desperate to invent the wheel again, you should always think ofSwigYou can replace the C/C ++ wheel with your language.