LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to export a class in a shared library (https://www.linuxquestions.org/questions/programming-9/how-to-export-a-class-in-a-shared-library-754902/)

DEF. 09-13-2009 04:35 PM

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?

nadroj 09-13-2009 04:57 PM

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++)?

DEF. 09-14-2009 05:34 AM

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.

xhypno 09-14-2009 06:53 AM

Quote:

Originally Posted by DEF. (Post 3682054)
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.

nadroj was correct. even when dynamicly loading, you must link against the so.

DEF. 10-04-2009 05:51 AM

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?

haikan 10-04-2009 06:50 AM

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

Sergei Steshenko 10-04-2009 01:01 PM

Quote:

Originally Posted by haikan (Post 3707077)
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

Just wondering - maybe BOOST already has some kind of convenience wrapper for all this ?

haikan 10-04-2009 02:13 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3707409)
Just wondering - maybe BOOST already has some kind of convenience wrapper for all this ?

DISCLAIMER: I have very little knowledge about boost.
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.