LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-13-2009, 04:35 PM   #1
DEF.
Member
 
Registered: Apr 2009
Posts: 93

Rep: Reputation: 23
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?
 
Old 09-13-2009, 04:57 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
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++)?
 
Old 09-14-2009, 05:34 AM   #3
DEF.
Member
 
Registered: Apr 2009
Posts: 93

Original Poster
Rep: Reputation: 23
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.
 
Old 09-14-2009, 06:53 AM   #4
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by DEF. View Post
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.
 
Old 10-04-2009, 05:51 AM   #5
DEF.
Member
 
Registered: Apr 2009
Posts: 93

Original Poster
Rep: Reputation: 23
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?
 
Old 10-04-2009, 06:50 AM   #6
haikan
LQ Newbie
 
Registered: Jul 2009
Location: Ålesund, Norway
Distribution: Slackware, Linux Mint
Posts: 21

Rep: Reputation: 2
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
 
Old 10-04-2009, 01:01 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by haikan View Post
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 ?
 
Old 10-04-2009, 02:13 PM   #8
haikan
LQ Newbie
 
Registered: Jul 2009
Location: Ålesund, Norway
Distribution: Slackware, Linux Mint
Posts: 21

Rep: Reputation: 2
Quote:
Originally Posted by Sergei Steshenko View Post
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...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ldd reports shared library missing, but library exists on disk athv_gr Linux - Newbie 7 05-13-2009 12:31 PM
gcc link shared library against another shared library qcp Linux - Newbie 1 07-25-2008 11:15 AM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
how to export a class(g++) onnyloh Programming 4 07-07-2004 06:30 AM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM


All times are GMT -5. The time now is 01:46 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration