Mix C and C++ with JNI
I did it as follows:
* I implemented an intermediate class in C++ that has a set of functions in C, which call the functions in C++. For instance, let's suppose you have a function func1 that you want to use from JNI. I implemented
This was the .c file. The .h file was like:
The real trick is to add the extern to the c++ functions in such a way that the C functions can use them.
* I had to build the jni wrapper full of JNIEXPORT, JNICALL, JNIEnv, etc, the usual way, save that I used my Arhuaco's api that took the .h and generated the whole wrapper from it. Anyway,
if you do it by hand, you can use javah to generate the .h from the .java with native keys.
(see JNI book).
*. To compile all the C++ code with g++
*. To compile the jni wrapper with gcc
*. To compile all the classes and the wrapper inside the shared object
* I implemented an intermediate class in C++ that has a set of functions in C, which call the functions in C++. For instance, let's suppose you have a function func1 that you want to use from JNI. I implemented
extern int func1_cplus (int attrib1, int attrib2);
extern "C" int func1 (int attrib1, int attrib2){
return func1_cplus(attrib1, attrib2);
}
int func1_cplus (int attrib1, int attrib2){
MyClass01 tst = new MyClass01();
}
This was the .c file. The .h file was like:
int func1( int attrib1, int attrib2);
The real trick is to add the extern to the c++ functions in such a way that the C functions can use them.
* I had to build the jni wrapper full of JNIEXPORT, JNICALL, JNIEnv, etc, the usual way, save that I used my Arhuaco's api that took the .h and generated the whole wrapper from it. Anyway,
if you do it by hand, you can use javah to generate the .h from the .java with native keys.
(see JNI book).
*. To compile all the C++ code with g++
g++ -c -fPIC XX.cpp -o XX.o $(CFLAGS)
*. To compile the jni wrapper with gcc
gcc -c fPIC -o libWrapper.o libWrapper.c $(CFLAGS)
*. To compile all the classes and the wrapper inside the shared object
g++ -shared -o libWrapper.so *.o
0 Comments:
Post a Comment
<< Home