Reputation: 40541
i have the following getmacip.cpp
file with the following snipp code
// ...code
#include "test.h"
// ...code
Test obj;
obj.printDem();
// ...code
here is the test.h
file
#ifndef TEST_H
#define TEST_H
class Test
{
public:
void printDem();
};
#endif
here is the test.cpp
file
#include <iostream>
using namespace std;
void printDem()
{
cout << "deus.ex.machina." << endl;
}
i am using java and c++ together via jni. i have gotten the file getmacip.cpp to work together with java and as you can see, i am trying to get the invoke the printDem() method from the getmacip.cpp file. i first compile the getmacip.cpp with the following:
g++ -fPIC -o libgetmacip.so -shared -I $JAVA_HOME/include -I $JAVA_HOME/include/linux getmacip.cpp -lc
this works fine. however when i do the same with test.cpp and run the program i get the error
/usr/lib/jvm/java-8-oracle/bin/java: symbol lookup error: /home/karl/workspace/sslarp/lib/libgetmacip.so: undefined symbol: _ZN4Test8printDemEv
i am obviously linking the files wrongly!
Upvotes: 0
Views: 87
Reputation: 20130
You could check what kind of symbols are here
$ nm -D libgetmacip.so
will produce list of symbols
If you have to demangle C++ names, use c++filt
$ nm -D libgetmacip.so | c++filt
check "man nm" for info how symbols are marked/used
Upvotes: 1
Reputation: 17455
The definition of the printDem()
doesn't belong to class Test
.
Use
#include <iostream>
#include "test.h"
using namespace std;
void Test::printDem() {
cout << "deus.ex.machina." << endl;
}
Also you should link
the method definition into the shared library, something like this:
g++ -fPIC -o libgetmacip.so \
-shared -I $JAVA_HOME/include -I $JAVA_HOME/include/linux \
detmacip.cpp test.cpp
Also I should mention that raw JNI is a plain-C interface, that's all native calls are performed to functons declared as C functions (their implementation may use C++, check this tutorial. Also there's SWIG projects which helps to create required wrappers for C++ automa{g,t}ically.
Upvotes: 2