SWIG-mixed programming with Java
Mixed Programming with Java-SWIG
Last update date: 2014-04-
Prerequisites: It is recommended that you have read "getting started with mixed programming _ SWIG with C #" and have the experience of compiling JavaProject in Eclipse.
Author:Kagula
Environment: Windows 8.1 64bit (English version), VisualStudio 2013 Update1 (English version), SWIG Win 3.0.0
JDK 1.6.0 _ 45 64bit, eclipse-jee-kepler-SR1-win32-x86_64
Body:
Create a C ++ project to test Java's call to it
[Main Menu]-> [NewProject]-> [Visual C ++]-> [Win32]-> [Win32 Project] Name the project SWIG_Tutorial4, and then confirm, select DLL for ApplicationType, additional Options select other default values of Empty Project.
Add a HelloWorldFromC. h file. The file content is as follows:
# Pragma once/* function: demonstrate Java's basic call to C ++ Last Updated Date: Environment: Windows 8.1 64bit, Visual Studio 203 Update1, SWIG Win3.0.0 note: for the SWIG interface definition file (I file), the biggest difference between the C # interface generation and the Java interface is that SWIG is called. the EXE program uses the-csharp parameter and the-java parameter, so the knowledge of C # is also applicable here. For more complex calling methods, refer to C ++ and C # mixed programming knowledge. Or the example under the "... \ swigwin-3.0.0 \ Examples \ java" folder, assuming that your SWIG runtime directory is under the "D: \ SDK \ swigwin-3.0.0" path. * // * Test Java's call to the C ++ object Method */class HelloWorldFromC {public: int add (int a, int B );}; /* test Java's call to C ++ global functions */int multiply (int a, int B );
Add the HelloWorldFromC. cpp file. The file content is as follows:
#include "HelloWorldFromC.h"int HelloWorldFromC::add(int a,int b){return a + b;}int multiply(int a, int b){return a*b;}
In this demo, there are two original code files of the C ++ project, which have been created now.
VisualStudio Win32 Project adopts Win32 mode by default. Now we change it to 64-Bit mode: select a Project in [Solution Explorer], and press the shortcut key [Alt] + [Enter] to open the Project properties page. Click the [Configuration Manager...] button in the upper-right corner to open the ConfigurationManger dialog box and select In the Type or select the new Platform in the "NewSolution platform" dialog box, select "x64" and then OK. In this way, the current project's Platform will change to "x64". Click the [Close] button to Close the ConfigurationManager dialog box.
Because we use a 64-bit JDK, the default JavaProject created by Eclipse is also 64bit and can only call the 64-bit dynamic link library, therefore, we need to change the Platform of the C ++ project from Win32 to x64.
Create a Java project to call the C ++ function module.
Open Eclipse, [Main Menu]-> [File]-> [New]-> [Java Project]. Create a Project named SWIG_Tutorial4_Java. Create a new Class named "MyJavaClass ".
Compile the SWIG Interface
Open the SWIG_Tutorial4 project in Visual Studio and add the SWIG_Tutorial4. I SWIG interface definition file for it. The source file content is as follows:
% Module SWIG_Tutorial4/* the header file used in the C ++ project name */% {/* I file (such as the variable type) */# include "HelloWorldFromC. h "%}/* header file to be parsed by SWIG */% include" HelloWorldFromC. h"
In [Solution Explorer], select the [SWIG_Tutorial4. I] file and [Alt] + [Enter] shortcut to open its properties page. [Configuration Properties]-> [General]-> [Item Type], select [CustomBuild Tool], click OK, and then open the Properties page of The SWIG_Turotial4. I file, [Configuration Properties]-> [Custom BuildTool]-> [General]-> [Command Line] enter the following five lines in the input box:
Echo Invoking SWIG...
Echo SWIG_HOME = $ (SWIG_HOME)
Echo on
$ (SWIG_HOME) \ swig.exe-c ++-java-outdir "D: \ JWorkspace \ SWIG_Tutorial4_Java \ src" "% (FullPath )"
Echo off
Assume that "D: \ JWorkspace \ SWIG_Tutorial4_Java" is the storage path of the Java project you just created.
[Configuration Properties]-> [CustomBuild Tool]-> [General]-> [Outputs] enter the following line in the input box:
% (Filename) _ wrap. cxx
In this way, the SWIG_Tutorial4. I file will be called. EXE program, generate the SWIG_Tutorial4_wrap.cxx file under the current C ++ project path, and generate a java packaging file in the "D: \ JWorkspace \ SWIG_Tutorial4_Java \ src" directory.
Open the Properties page of The SWIG_Tutorial4 project in Visual Studio, and enter the following three lines in the [Configuration Properties]-> [Build Events]-> [Post BuildEvent]-> [Command Line] input box.
Echo on
Copy "$ (OutDir) $ (ProjectName). dll" "D: \ JWorkspace \ SWIG_Tutorial4_Java"
Echo off
In the [Configuration Properties]-> [Build Events]-> [Post BuildEvent]-> [Outputs] input box, enter the following line of description.
Copy native dll to java run directory
In this way, after C ++ Build is complete, the DLL file will be copied to the path "D: \ JWorkspace \ SWIG_Tutorial4_Java.
After compiling the SWIG_Tutorial4. I file, add the generated SWIG_Tutorial4_wrap.cxx file to the current C ++ project and RebuildC ++ project.
Modify Java source code and test the C ++ DLL Project
Open the SWIG_Tutorial4_Java project under Eclipse. After refreshing the project, you can see SWIG in [Package Explorer. the HelloWorldFromC generated by EXE for you. java, SWIG_Tutorial4.java, SWIG_Tutorial4JNI.java, and modify MyJavaClass. the content of the java file is as follows.
Public class MyJavaClass {static {// enter the name of the C ++ project and the DLL name System written in C ++. loadLibrary ("SWIG_Tutorial4");} public static void main (String [] args) {/* print libXXX. so file search path: System. out. println ("java. library. path = "+ System. getProperty ("java. library. path "); */HelloWorldFromC objC = new HelloWorldFromC (); System. out. println ("Test C ++ object method call =>" + objC. add (1, 2); System. out. println ("Test C ++ global function call =>" + SWIG_Tutorial4.multiply (2, 3 ));}}
Now you can see the running result in the Eclipse Console window when running the Java file. To view the running Process of C ++ code, you can Attach the Process to the Java Process in Visual Studio and track the dynamic link library written in C ++ in one step.
Appendix: List of cmakelists.txt files used for transplantation to centos
# Set the PROJECT name PROJECT (SWIG_Tutorial4) # The minimum VERSION of CMake is 2.8CMAKE _ MINIMUM_REQUIRED (VERSION 2.8) # Add the header file search path # The following two are the default header file paths of Java Development kits under Cent OS: INCLUDE_DIRECTORIES (/usr/lib/jvm/java/include) INCLUDE_DIRECTORIES (/usr/lib/jvm/java/include/linux) # Add the library file search path # The following two are the default library file paths of Java Development kits under Cent OS LINK_DIRECTORIES (/usr/lib/jvm/java/lib) # stores the names of all source files in the current directory in the AUX_SOURCE_DIRECTORY (. DLL_SRC) # used to specify the source file source1 source2... SourceN (defined in the variable DLL_SRC) # compile the dynamic link library for the name "libSWIG_Tutorial4.so" based on the name "SWIG_Tutorial4" # If SHARED is not added below, compile the static Link Library "bytes" ADD_LIBRARY (SWIG_Tutorial4 SHARED $ {DLL_SRC })