Mixed programming with Java-SWIG
Last updated on : 2014-04-20
Read the premise : it is recommended that you have read "Getting started with mixed programming with C # _swig", with the experience of writing javaproject under Eclipse.
Author: Kagula
Environment : Windows 8.1 64bit (English version), VisualStudio Update1 (English), SWIG Win 3.0.0
, JDK 1.6.0_45 64bit, eclipse-jee-kepler-sr1-win32-x86_64
Body:
New C + + engineering to test Java calls to it
[Main Menu]->[newproject]->[visual C++]->[win32]->[win32 Project] give project a name of Swig_tutorial4 after confirmation, Applicationtype Select DLL, Additional options Choose Empty Project Other default.
Add the HelloWorldFromC.h file with the following file contents
#pragma once/* Features: Demo Java basic call to C + + Last updated: 2014-04-19 environment: Windows 8.1 64bit, Visual Studio 203 Update1, SWIG Win3.0.0 Note: For the Swig interface definition file (i file) to generate the C # interface with the Java interface The biggest difference is to call the SWIG.EXE program one with the-csharp parameter one with the-java parameter, so the knowledge of C # is also applicable here. More complex invocation forms, you can refer to C + + and C # mixed programming knowledge. or "... \swigwin-3.0.0\examples\java" folder under the example, this assumes your swig run directory under the "D:\SDK\swigwin-3.0.0" path. *//* tests Java calls to C + + object methods */class helloworldfromc{public:int Add (int a, int b);};/ * Test Java calls to C + + global functions */int multiply (int a, int b);
Add the HelloWorldFromC.cpp file with the following file contents
#include "HelloWorldFromC.h" int helloworldfromc::add (int a,int b) {return a + B;} int multiply (int a, int b) {return a*b;}
This demo program of C + + engineering original code file total two, and now all have been established.
VisualStudio Win32 Project defaults to Win32 mode, now we change it to 64-bit mode: Select the item in [Solution Explorer], and the shortcut [Alt]+[enter] opens the project Properties page. In the top right-hand corner, click [Configuration Manager ...] button to open the Configurationmanger dialog box, select <new...> in the active solution Platform, and in the Newsolution Platform dialog box, type or select the OK after selecting "x64" in new platform. This way, the current item's platform changes to "x64", and the mouse clicks the [Close] button and closes the ConfigurationManager dialog box.
Because we are using a 64-bit JDK, so the default Javaproject built by Eclipse is 64bit and can only call 64-bit dynamic link libraries, so we need to change the C + + project platform from Win32 to x64.
Now create a new Java project to invoke the C + + function module
Open eclipse,[main Menu]->[file]->[new]->[java Project] The new project is named Swig_tutorial4_java. Create a new class named "Myjavaclass".
Writing the Swig interface
Open the SWIG_TUTORIAL4 project in Visual Studio, add the SWIG_TUTORIAL4.I Swig interface definition file to it, and the contents of the source file are as follows:
%module SWIG_TUTORIAL4/* Here to set the header file to use for C + + project name */%{/* I file (for example, variable type) */#include "HelloWorldFromC.h"%}/* SWIG header file to parse */% Include "HelloWorldFromC.h"
In [Solution Explorer], select the [swig_tutorial4.i] file, [alt]+[enter] shortcut, and open its property page. [Configuration properties]->[general]->[item Type], select [Custombuild Tool], click OK, then open the Swig_turotial4.i file's property page, [ In the Configuration properties]->[custom buildtool]->[general]->[command line] input box, enter the following 5 lines:
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
This assumes that " D:\JWorkspace\SWIG_Tutorial4_Java" is the storage path for the Java project you just built.
Enter the following line in the [Configuration properties]->[custombuild tool]->[general]->[outputs] Input box:
% (Filename) _wrap.cxx
This compiles the swig_tutorial4.i file and invokes the SWIG.EXE program, which generates the Swig_tutorial4_wrap.cxx file under the current C + + project path, in theD:\JWorkspace\SWIG_ Tutorial4_java\src "directory, generate Java wrapper file.
Open the SWIG_TUTORIAL4 Project's property page in Visual Studio, [Configuration properties]->[build events]->[post buildevent]->[ Command Line] In the input box, enter the following three lines of content
echo on
Copy "$ (OutDir) $ (ProjectName). dll" "D:\JWorkspace\SWIG_Tutorial4_Java"
echo off
Enter the following line to describe the information in the [Configuration properties]->[build events]->[post buildevent]->[outputs] Input box
Copy Native DLL to Java run directory
This will copy the DLL file to the "D:\JWorkspace\SWIG_Tutorial4_Java" path when the C++build is complete.
After compiling the swig_tutorial4.i file, add the generated swig_tutorial4_wrap.cxx file to the current C + + project, rebuildc++ project.
Modify Java source code and test C + + DLL Engineering
After you open the Swig_tutorial4_java project under Eclipse and refresh the project, you can see the Helloworldfromc.java, Swig_ generated by SWIG.EXE for you in [package Explorer]. Tutorial4.java, Swig_tutorial4jni.java three Java files (interface wrapper file), modify the contents of the Myjavaclass.java file as follows.
public class Myjavaclass {static {//Here is the name of the C + + project, and also the name of the DLL written by C + + system.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));}}
Running the Java file now allows you to see the results of the run in the console window of Eclipse. To see how the C + + code runs, you can attach process to Java processes in Visual Studio and step through the dynamic-link libraries written by C + +.
Attached: List of CMakeLists.txt files used for porting to CentOS
#设置项目名称PROJECT (SWIG_TUTORIAL4) #要求CMake的最低版本为2.8cmake_minimum_required (VERSION 2.8) #添加头文件搜索路径 # Two below is cent The default header file path for the Java SDK under OS include_directories (/usr/lib/jvm/java/include) include_directories (/usr/lib/jvm/java/include/ Linux) #添加库文件搜索路径 # The following two are the default library file path for the Java SDK under Cent OS Link_directories (/usr/lib/jvm/java/lib) #用于将当前目录下的所有源文件的名字保存在变量 Dll_ SRC in Aux_source_directory (. DLL_SRC) #用于指定从一组源文件 source1 source2 ... sourcen (defined in variable dll_src) #根据 "SWIG_TUTORIAL4" name to compile a dynamic-link library of "libswig_tutorial4.so" name #如果下面不加SHARED那编译出的是 "libswig_tutorial4.a" Static link library add_library (swig_tutorial4 SHARED ${dll_src})
SWIG-Mixed programming with Java