Liunx Java-eclipse the call to the so library of C + + (top) __c++

Source: Internet
Author: User

Welcome reprint, Reprint please explain.

Many of the Internet use Java to invoke C/s + + 's so library, but most of the examples are a simple C language file, and then compiled to play the library.

These examples can only bring us to know how to play the library and call steps, in the actual project process, encountered is not the imagination of what is simple.

Below I will take a C + + example to demonstrate the whole process from the library to the call, I hope for beginners useful, prawns, Daniel side over, welcome onlookers.

Example:

There is a car class interface icar, which declares several basic methods, while having a phorsche (Bostier) inheritance icar, and implements the corresponding interface, which uses the basic data type and the type of the structure, The goal is to use this example to practice the interface when encountering different types of parameters in the actual project, and it is possible to invoke it directly without the need for conversion.

Interfacecar.h

#ifndef icar_h #define ICAR_H #include "string" typedef struct tagengine{char name[20];
	Char date[20];
	Char address[100];
	int number;
	Double width;
	Double height;
BOOL Isrepair;

}engine,*pengine;
	Class ICar {public:icar (void);

	~icar (void);

	Enum Carstate {Csstop,csrunning,csstart,csbad};
	virtual void Setcarname (const std::string& Carname) = 0;
	Virtual std::string getcarname () = 0;
	Virtual Carstate getcurrentstate () = 0;
	virtual void SetState (carstate cs) = 0;
	virtual void Getengine (engine* Engine) = 0;

	Virtual Engine getengin () = 0;
	virtual void stop () = 0;
	virtual void start () = 0;
	virtual void run () = 0;
virtual void Breakit () = 0;

};
	 Class Phorsche:public ICar {Public:phorsche ();
	 ~phorsche ();
	 void Setcarname (const std::string& carname);
	 std::string getcarname ();
	 Carstate getcurrentstate ();
	 void SetState (Carstate cs);
	 void Getengine (engine* Engine);

	 Engine Getengin ();
	 void Stop ();
	 void Start ();
	 void run ();
void Breakit (); Private:carstaTe M_state;
	Engine M_engine;
Std::string M_name;

}; #endif


Interfacecar.cpp

#include "interfacecar.h" #include "iostream" #include "string.h" Icar::icar (void) {} icar::~icar (void) {} void Phor

Sche::setcarname (const std::string& carname) {m_name = Carname;}
	Phorsche::P Horsche () {strcpy (m_engine.address, "GZ");
	strcpy (M_engine.date, "2012-11-2");
	M_engine.height = 23.4;
	M_engine.width = 56.2;
	strcpy (M_engine.name, "Phorsche");
	M_engine.isrepair = false;
M_engine.number = 120042912;

} phorsche::~phorsche () {} std::string Phorsche::getcarname () {return m_name;}

Icar::carstate phorsche::getcurrentstate () {return m_state;}

void Phorsche::setstate (Carstate cs) {m_state = cs;}
	void Phorsche::getengine (engine* Engine) {strcpy (engine->address, m_engine.address);
	strcpy (Engine->date, m_engine.date);
	Engine->height = M_engine.height;
	Engine->width = M_engine.width;
	strcpy (Engine->name,m_engine.name);
	Engine->isrepair = M_engine.isrepair;
Engine->number = M_engine.number; } Engine Phorsche::getengIn () {return m_engine;} void Phorsche::stop () {std::cout<< "The car is stop."
	<<std::endl;
M_state = Csstop; } void Phorsche::start () {std::cout<< "the car is start."
	<<std::endl;
M_state = Csstart; } void Phorsche::run () {std::cout<< "is running."
	<<std::endl;
M_state = csrunning; } void Phorsche::breakit () {std::cout<< "is breakit."
	<<std::endl;
M_state = Csbad;
 }


Here's how to play the so library and invoke the procedure.

Preparatory work:

If it is a Linux environment: Please install GCC, g++,jdk,swig,[eclipse], the bracket can not be installed, for debugging purposes. Of course, you can also use the command line.

If the win platform: Please press Cygwin,swig, JDK.

I'm going to do two things in Linux and Windows:

First on the introduction of Linux (younger brother is a Linux rookie, the first time to play Linux, but also because of the process of playing the library XX after XX solution, to avoid a strong understanding of Linux a little bit):

Linux Environment:

Preparatory work:

Installation: Ubuntu + Eclipse + openjdk1.6.3

Installation: gcc/g++ 4.6.3 version

Installation: Swig 2.0.4 version

If not, open the terminal directly: Apt-get install <xx name > to automatically press the required software, (anyway, I put it here).

User name: xxxx is not the root permission, which has restrictions on subsequent copy files or deleted file operations. You can use the sudo su-command to elevate the user to root. I do not know other Linux can not, anyway, my Ubuntu system installed after the default is the user I set, for root I do not know the password, there is no way I can only use sudo su-to promote. If there are other ways to get the root password to ask the teaching brother.

Set up a good folder:/home/xxxx/demo where xxxx is my Linux user name, the path can be stored on their own, in this demo file to place the previous interfacecar.h and interfacecar.cpp files.

Ready to work OK.

1, first set up a. i file, this file is relatively important, it is by it to the C/C + + interface to Java call the interface. My name here is: cplus.i

Cplus.i (handwriting, do not know if there is no tool)

* FILE:CPLUS.I * *
%module Cplus
%{
#include "Interfacecar.h"
%}
%include "Interfacecar.h"

2. Use Swig to convert. i files to Java files.

Use command: swig-c++-java-package com.plus-outdir.//home/xxxx/demo/cplus.i

Description

-c++: Indicates that the C + + interface is required for Exchange. If you do not use this parameter, the default is to swap with the C interface.

-java: Switch to Java interface.

-package: Specify the name of the package, of course this is not necessary. According to their own habits. Delete this parameter if you do not use it.

-outdir: Used to specify the compiled file output path.

./: Indicates that the output directory is the current path.

The following is the path where the. i file is stored.

When the command is executed, the. Java and xxxx_wrap.cxx files are generated,

Where. Java is used when you look up a type when you call the interface later. And Xxxx_wrap.cxx is used to play the so library.

Put the resulting Java files in the test Engineering demo directory.

3, the use of CD instructions to enter the demo directory path.

Cd/home/xxxx/demo

4. Use the locate jni.h to view the directory where the jni.h resides, and use locate Jni_md.h to view the directory where the jni_md.h resides.

5. Compile CPP and cxx to generate. o Files (because the following. o to generate. So)

g++-wall-c interfacecar.cpp cplus_wrap.cxx-i/usr/lib/jvm/java-6-openjdk-i386/include-i/usr/lib/jvm/ Java-6-openjdk-i386/include/linux

This uses the-I (uppercase I) to connect the path to the header file.

Also note that here is C + + so to use g++ to make up, if with GCC, it is possible to find the declaration of error.

Two. o files will be produced (INTERFACECAR.O,CPLUS_WRAP.O)

6. Generate So Library

g++-share-fpic INTERFACE.O Cplus_wrap.o-o libcplus.so

Note: If you just use CPLUS_WRAP.O a file to play so, there may be problems with data types that cannot be found.

(This is a mistake, for the following explanation)

If you use an. o to connect later, the test run will report a similar error (I think this is due to dependencies not being called in)

The steps above are complete

The following is the Eclipse invocation demo, the Java master does not laugh, the first time using Eclipse.

Create a new Java project

Named Callcplus

Add the previously generated Java file to the test engineering directory, as shown in the following illustration.

Second, all of the. java files in the demo will be copy to the test engineering directory.

Writing test Code

Demo Calling code:

Package com.cplus;
         public class Callcplus {static {system.loadlibrary ("Cplus");
    System.load ("/home/xxxx/demo/cplus/libcplus.so"); }/** * @param args */public static void main (string[] args) {//TODO auto-generated Method St
        UB//system.out.print (System.getproperty ("Java.library.path"));
        int a = Example.fact (50);
        Double BB = example.getmy_variable ();
        
        String BB = Example.get_time ();
        Phorsche Pche = new Phorsche ();
        
        Pche.setcarname ("T M D");
        System.out.print (Pche.getcarname ());
        Pche.run ();
        swigtype_p_std__string OK = "Phorsche";
        
        Pche.setcarname (OK);
        
        Icar.carstate cs = pche.getcurrentstate ();
        Engine eg = Pche.getengin ();
        System.out.printf ("%s\n", eg.getaddress ());
        System.out.printf ("%s\n", Eg.getdate ());
        System.out.printf ("%s\n", Eg.getname ()); System.out.prinTF ("%f\n", Eg.getheight ());
        System.out.printf ("%f\n", Eg.getwidth ());
        System.out.printf ("%d\n", Eg.getnumber ());
        
        System.out.printf ("%b\n", Eg.getisrepair ());
        if (cs = = ICar.CarState.csRunning) {System.out.print ("OK");
 }
        
    }
}

If you use system.loadlibrary here, you remove Lib and. So, take only the middle name, and use the path if you use System.load (System.load ("/home/xxxx/libcplus.so").


To run the demo:

Error, (because of my eclipse to find the Java library path problem, the master himself set, I only know that the so copy to/usr/lib, I tried to put under the/usr/local/lib but not)

So use Cp/home/xxx/demo/libcplus.so/usr/lib

Note that root permissions are required

Finally, run again. OK;

Another problem is that I used the std::string in C + +, but I switched to swigtype_p_std__string when I swig the type, in Java using String. As a result, a string cannot to convert swigtype_p_std__string error was reported, and I checked, as if to convert the type in the. i file. Please teach this to Daniel.

This will be shown in the win platform package so.

It's too late to sleep.

Related Article

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.