System: CentOS 6.6
Steps:
1, installation Swig
Direct Yum-y Install Swig
Install Java and C + + compilation environment, skip
I directly yum-y install Java, found that the system has its own Java
2. Prepare source files and interface files
Before running Swig, you should first prepare the source file (example.c) of the C/D + + program and write the interface file (example.i). Here is a simple example, to encapsulate a module called example.
We want to export all the variables and functions in the example.c file. as shown below.
<span style= "FONT-SIZE:14PX;" >/* example.c */#include <stdio.h>int integer = 100;int Add (int a, int b) { return a + b;} char* getString () { return "Some string";} void PrintLine (const char* str) { printf ("%s\n", str);} </span>
Create a new file example.i, and enter the following:
<span style= "FONT-SIZE:14PX;" >%module Example%{extern int Integer;extern int Add (int a, int b), extern char* getString (), extern void PrintLine (const char* str);%}extern int Integer;extern int Add (int a, int b), extern char* getString (), extern void PrintLine (const char* St R);</span>
EXAMPLE.I in%module is the name of the library, and the content in%{and%} is the content to be added to the makefile, usually the definition of functions and variables and the header files to be included, so that the generated source files can be compiled by the daughter. The following content is the normal C function and the definition of the variable, the same as the normal C language syntax.
3. using Swig to generate Java class files
Run command now
Swig-java example.i
The default is to generate EXAMPLE_WRAP.C, Example.java, Examplejni.java three files.
Then compile the shared library file with the following command.
Gcc-c-fpic example.c example_wrap.c-i/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.35/include-i/usr/lib/jvm/ Java-1.6.0-openjdk-1.6.0.35/include/linux
Gcc-shared-o libexample.so EXAMPLE.O EXAMPLE_WRAP.O
Libexample.so is generated by default
Note that the compile time to include the Java local access to the oral file jni.h is located in the directory, the location of the file is different in different systems, compile needs to join.
4. Test procedure
Library functions can now be called using Swig generated class file Example.java and the corresponding JNI file Examplejni.java. Create a new file Main.java to test it.
public class main{public static void Main (String args[]) { system.loadlibrary ("Example"); System.out.println ("Getinteger ():" + Example.getinteger ()); Example.setinteger (+); System.out.println (Example.getinteger ()); System.out.println ("1 + 2 =" + Example.add (1, 2)); System.out.println ("getString ():" + example.getstring ()); Example.printline ("printstring ()");} }
compilation Before translating the libexample.so put into the system library contains the directory/lib or /usr/lib , execution cp libexample.so/usr/lib (you can also add libexample.so to
Compile run:
Javac Main.java
Java Main
The output results are as follows:
Getinteger (): 100
100
1 + 2 = 3
GetString (): Some string
Printstring ()
5. Summary
Swig is powerful and can support many languages. This is only a test of the Java language encapsulation, for other languages, the method is similar, but at compile time need to include the corresponding local library header files, such as Python in the Python.h,java in the jni.h. In addition, here is just the simplest interface file to be written, in fact, the interface file can have more options, these are in the Swig document more detailed description. C language encapsulation is relatively simple, for the object-oriented language such as C + +, the programming of the interface file is slightly complex, but also relatively easy to grasp. Of course, there is a problem when invoking these local interfaces, which is security. Be fully aware of security issues when designing interfaces, to prevent pointer errors and memory leaks, and so on. Although Swig has been able to handle pointers well, sometimes it is necessary to implement the encapsulation of pointers.
Using Swig to port A/C + + library to Java under Linux