how to export a class in a shared library
I have a c++ shared library interface defined something like as follows:
#include "ClassA.h" extern "C" ClassA* getClassAPtr(); and an executable using it something like this: #include "ClassA.h" typedef ClassA*(*FUNCTPTR)(); // load shared library stuff etc... FUNCTPTR p_funcPtr = dlsym // etc... ClassA* p_classa = (*p_funcPtr)(); It compiles fine but I get a link error as follows: undefined reference to 'ClassA' OK, so I can solve this by also including the source for class A in my executable, but that seems silly, thus why bother with the shared library. Is their a method to export the class form the library? |
im not familiar with writing libraries in C++, but i do understand the concepts. can you show the command and arguments your using to compile/link your code? are you passing your library when you link, something like "-lmylibrary" (assuming g++)?
|
No the lib is dynamically linked thus I use dlload, dlsym etc...
The problem is that the library does not export the Class's output by one of the library exported functions. One way to fix is to include the library source in the application using the library. But his is silly. So the question is how to export a Class from a shared library. |
Quote:
|
xhypno and nadroj: Really!? That cannot be right. What is the point in having an .so if you need to link it in at compile time. Surely you just include the header file and that's it?
|
Normally you specify the name of the shared libraries you want to link against as arguments to the linker. Then the library is loaded and automagically linked with your executable at runtime.
dlopen, dlsym and friends are used for example with plugins. You can load the library with dlopen, and resolve function pointers from names with dlsym. I suspect you know all this, so to address your question regarding dlopen in combination with C++, I'd sugest you'd read the "C++ dlopen mini HOWTO" at http://www.faqs.org/docs/Linux-mini/C++-dlopen.html |
Quote:
|
Quote:
I recall having read somewhere about boost shared_library (shared_library :: open(), shared_library::call(), etc.). I'm sure google has more on the subject... |
All times are GMT -5. The time now is 01:44 PM. |