Swig, C/C ++ class libraries and universal adhesives for Java, Python, and other general high-level languages

Source: Internet
Author: User

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.

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.