Language binder-swig

Source: Internet
Author: User
What is swig?  Swig, namely, simplified encapsulation and interface generator (simplified wrapper and interface generator). According to its creator, is a software development tool that connects programs written in C, C ++, and objective-C to various advanced programming languages ". UNIX, windows, and Macintosh systems all support swig. For Java programmers, swig is an ideal tool to embed C/C ++ code into java scripts. Swig understands C/C ++ statements, so it can parse the C/C ++ header file and generate a Java model and encode it. After compilation, you can use it in Java. Swig is actually an "interface file" as input, but it is composed of several (optional) Standard C/C ++ header definitions. In fact, if the code is fairly clean, you can use the header file itself. The generated code is complete (that is, there is nothing left for you to fill in), and the Code consists of a Java model and a C implementation file, which only needs to be compiled. Swig Supported languagesSwig supports a wide range of languages and supports almost all the currently popular languages, currently, the C/C ++ conversion languages are Java/C #/PHP/Python/perl/perl5/Ruby/ocaml/Tcl/chicken. For detailed usage of various languages, refer to the user manual. The following uses Java as an example to describe how to use swig to integrate C ++ programs. (Detailed descriptions are provided in the swig manual ). Download and install swigDownload the latest swig program from the official website or open-source project and decompress it to the local directory. Install JDK And add JNI PackageDownload the latest Java JDK and JNI Development kits from Sun's official website. Use vs6.0 Perform swig ConfigurationL open a new project and create a DLL project through the wizard. L add a swig interface file (usually ended with. I, such as example. I) to the project, and the encapsulated file (ie. example_wrap.c) That swig will generate ). Note that if you use C ++ to select different suffixes for the encapsulated file example_wrap.cxx, Do not worry that the encapsulated file does not exist-Vs will automatically update it. L select the swig interface file (example. I), press right, and select the "Settings" option in the pop-up menu. L enter "swig" in description ". L in the commands text box, enter 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 )". Swig.exe file directory defined in environment variables. L in the outputs text box, enter $ (inputname) _ wrap. cxx, indicating that according to the swig interface file (example. (I) generate the corresponding C ++ encapsulation file. The generated encapsulation file name is example_wrap.cxx. L select the entire project, select the C/C ++ attribute page, and select the Preprocessor item of "category. In "addition include directories", enter the directory containing the Java and JNI header files, java_include, java_include/Win32. L then select the link property page and select "general" of "category. Set the name of the output DLL file in out put file name. L finally, on the select post-build step attribute page, add the Java file compilation command % java_bin %/javac *. Java in post-build command (s. L The entire setup process has been completed. It should be noted that the JNI function interface generated is defined by the swig interface file (example. (I) defined together with the method name. In the interface file, the module name is % module example, and the method name of C ++ is used together. For example, if the method start () exists in C ++, The java_examplejni_start () described in JNI is as follows (), if the interface file is defined as % module example_test, the function interface definition in JNI is changed to java_example_testjni_start (). At the same time, the definition of the JNI function interface also requires that the package structure of the called Java program correspond to it. L compile and run. Swig Resources  Official Website: http://www.swig.org open source project: http://sourceforge.net/projects/swig/

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.