I got stuck with c++. I do Java since more than 15 years, but c++ since a couple of weeks only. So, doing c++ is influenced by Java, no question! But as c++17 goes into this direction too, it may not be completely false. Here is my case: I need a shared library containing a class called MyListener abstracted as:
class MyListener {
private:
static int field1;
static struct field2;
public:
~MyListener() {
// do whatever!
}
//constructor
MyListener() {
field1 = 10;
field2.name = “Hello”;
field2.addr=”Dolly”;
}
Vector<char> getMessage() {
Open socket using initialized static fields;
return socket.message;
}
};
And I need a Test class, to use the shared library, let’s say MyListenerTest.cpp containing something like:
int main() {
MyListener * ml = new MyListener();
while(true){
std::vector<char> data = ml->getMessage();
}
delete ml;
}
Question:
- The Class MyListener can be compiled and a shared library like libMyListener.so is created.
To compile the Test class I defined something like
g++ -std=c++14 -LpathtoMyListener –lMyListener -etc
But the class MyListener can not be seen from MyListenerTest.cpp How to tell MyListenerTest where to look for? Of course, first I declared the class in a header file and included this file in the test program, so the class is seen. But either I get a defined twice error, or if I declare the class in the header only, I get an error w.r.t the private fields not being visible. Of course I tried also to use setters and getters as it’s common use in Java. But this gave a horrible solution!
- How to handle the case in a clean way with c++?
At the end the shared library shall be accessed from Java through JNI. For now I stick to c++ for test. Working on Debian9 and Eclipse Oxigene.