Using Swig to convert C + + interfaces to Java interfaces

Source: Internet
Author: User
Tags set set

Take C + + class classifier as an example, file saved in Baidu Network disk Https://pan.baidu.com/s/1c2AwhaS (need password)

System: Ubuntu 15.04

Resources:

Ubuntu Source Installation Swig

Convert C + + interface to Java interface using Swig

SWIG3.0 Description Document

Errors that occur in programming:

Java compilation appears undefined symbol: ... Most of this is due to the need to add a link to a lib....so file in the original C + + library (undefined symbol:

_zn5boost6system15system_categoryev)

Java compile-time error:could not find or Load main class test: This is because of javac-cp/home/fiona/20160419/sg_fortestsucc/swig/java/ Myclassifier.jar Test.java The local directory is not added, it should be javac-cp.:/ Home/fiona/20160419/sg_fortestsucc/swig/java/myclassifier.jar Test.java (error:could not find or Load main class [ Duplicate])

Body:

0. C + + Dynamic Library compilation

Compile classifier.cpp into C + + dynamic library libclassifier.so and put the dynamic library libclassifier.so into the system's library file

Figure 1 Compiling C + + dynamic libraries

1. Using Swig to convert C + + interface to Java interface

1.1 Swig interface file (. i) for writing and using

@ Swig need to write an interface file with the suffix. I to specify the interface functions that need to be exported from the C + + class;

Figure 2 Swig's interface file

The module name is given by the specified%module (or with the-module command-line option). This indicative text must be written in the head 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.

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.

Transformation of Stl/c++ Library
This section of the Library module provides access to some standard C + + libraries including STL methods. Making Swig support STL is an ongoing endeavor.
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
The table should say that it is not perfect. Some language modules support a subset of the above and other STL classes that support extensions. Please look carefully for the library files in the relevant language library directory.

@ based on a well-written. i file, use the Swig command to generate a wrap file for Java classes and C + + interfaces

Figure 3 Swig command

Execute $swig-c++-java-package com.classifier-outdir./-I.. /MYCLASSIFIER.I

Swig parameter Description:

1)-c++-java

Tells Swig to convert the C + + interface to a Java interface. If the C interface is converted to Java interface, there is no need to-c++, write Swig-java directly can;

2)-package

The name of the package that generated the Java class;

3)-I.

The path to the. h file of the include in Myclassifier.i.

After executing the SWIG command, several files are generated under the Swig path:

1) myclassifier_wrap.cxx

C + + files, wrapper files. It converts the C + + class method to the C function;

2) Myclassifier.java

Java class with the same name as the module defined in Swig;

3) Myclassifierjni.java

Methods in the C + + class are converted to Java static methods in this file;

4) Classifier.java

A Java class with the same name as the C + + class that contains the member functions that need to be exported;

5) Swig_....java

Swig the generated Java class for a custom C + + type conversion

1.2 Compiling the Myclassifier_wrap.cxx file as a. So library file

Required files: jni.h, jni_md.h, just compiled C + + library file libclassifier.so

Figure 4 Generation of C + + libraries used by JNI

The resulting. So file needs to be placed in the System Library folder, and JNI will call the classes and methods in the Libclassifier.so library through the libmyclassifier.so library.

So far, the C + + interface has been converted to a Java interface via swig.

2. Java interface generated using Swig

2.1 Compiling Java files

Entering the Swig directory, the Java files generated by Swig are now here.
$javac *.java Generate class file = "

The Java package we set up just now is com.classifier, so we create the Com/classifier directory, move the class file to that directory and package

$jar-CVF Myclassifier.jar./com Generate Myclassifier.jar Package

Figure 4 Compiling a Java file

2.2 Testing

The Myclassifier.jar package needs to be imported, and the library files to be used are placed under the System's Library folder or in the specified location

Figure 5 Java Test Program

Figure 6 Java compilation directives

At this point, the C + + interface is successfully converted to Java interface using Swig.

Using Swig to convert C + + interfaces to Java interfaces

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.