Answer the question
In order to leave comments, you need to log in
Accessing a c++ method of an object from c
It is necessary to call the c++ method of the object from the c file, which is already declared and described in the c++ file.
The problem is that on the Internet I found only examples with method calls only for newly created objects (that is, those created in a wrapper file).
So, there is:
Actually cpp files with the desired object:
blah.h:
class blah {
public:
int method (void);
}
extern blah object;
#include "blah.h"
int blah::method (void) {
;
}
blah object;
extern "C" int wrapper_method ( void ) ;
#include "wrapper.h"
#include "blah.h"
int wrapper_method ( void ) {
return object.method ();
}
Answer the question
In order to leave comments, you need to log in
I recommend looking at the implementation of JNIEnv in jni.h.
From C, its methods are called like this: (*env)->callMethod(env, data)
From C++ like this: env->callMethod(data)
Try to bind the function in the wrapper using boost::bind or std::mem_fun
boost::bind<blah>(&blah::method, &obj);
std::mem_fun<void, blah>(&blah::method);
Everything links fine (gcc-4.6)
iBolit# make
g++ -c -o blah.o blah.cc
g++ -c -o wrap.o wrap.cc
cc -c -o main.o main.c
cc blah.o wrap.o main.o -o blah
iBolit# ./blah
Blah!
iBolit#
I specifically made main.c to check the link between C and C ++ modules, everything works. Most likely you have something wrong in the compilation / linking process, for example, the blah.o file was undeservedly forgotten during linking.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question