lundi 5 décembre 2016

Call C++ functions from Java

This is my first post on StackOverflow. I am working on a project and I need your help in JNI. I got stuck there... I have been reading and trying it a lot recently, but still didn't figure out how to make it work.

I have created a static library in C++ (a pseudo-code is shown below):

// file: X.h    
class X {
    public:
        X() {};
        ~X() {};

        void fooX() { // do stuff };
        void barX() { // do more stuff };
}

// file: X.cpp
#include "X.h";

// file: Y.h 
#include "X.h"
class Y {
    public:
        Y() {x = new X()};
        ~Y() {};

        fooY() { // do stuff };
        barY() { // do more stuff };

    private:
        X x;    // object of class X
}

// file: Y.cpp
#include "Y.h"

// file: Z.h 
#include "Y.h"
class Z {
    public:
        Z(uint8_t, std::string, std::vector<uint8_t>);
        ~Z() {};

        fooZ() { // do stuff };
        barZ() { // do more stuff };

    private:
        Y y;    // object of class Y
}

// file: Z.cpp
#include "Z.h"
Z::Z(uint8_t a, std::string b, std::vector<uint8_t> c)
{
     /// do stuff and create an object of Y
     Y y = new Y();
}

// file api.h
#include "Z.h"
void accessZ(uint8_t, std::string, std::vector<uint8_t>);

// file api.cpp
#include "api.h"
void accessZ(uint8_t a_uint, 
     std::string b_string, 
     std::vector<uint8_t> c_vector)
{
     // create object of Z
     Z z = new Z(a_uint, b_string, c_vector);
     z->fooZ();

     delete z;
     z = NULL; 
}

Further, I have compiled all of the above code as a static library (libXYZ.a) in C++ (Eclipse C++ CDT on Windows 7 using MinGW).

Now, what I would like to have is to be able to call the API accessZ() C++ function from a Java application! In other words, I have implemented the core functionality in C++ and I would like to implement the GUI in Java. Therefore, I need to access the C++ functions from the Java GUI...

How can I do that? I need direct answers and not vague ones please...

Your help is very appreciated.

Thanks a lot in advance for your support.

Aucun commentaire:

Enregistrer un commentaire