LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 06-17-2009, 07:39 AM   #1
lennyk
LQ Newbie
 
Registered: Jan 2009
Posts: 18

Rep: Reputation: 0
Linking an executable to a shared library (C++)


Hi,

I've written a shared library (in C++) I've built using libtool (through the automake/autoconf system).

I've also written an application (C++ executable) that dynamically loads this library using dlopen.

I would like the shared library to call a function implemented in the executable.
How do I link the shared library to the executable?

(I've tried adding the executable to the linking stage of the shared library automake system - but this didn't work)

Thanks!
 
Old 06-17-2009, 03:41 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,408

Rep: Reputation: 108Reputation: 108
It doesn't work. What you may try is to add the object file from the executable to the library. Or, if that solution does not suit you, you may add a new function to your library that will get a function pointer from the application. Then the pointer (pointing to the function in question, of course) may be used in the library. Or...maybe move the function that calls the function in question to the executable?
 
Old 06-17-2009, 04:20 PM   #3
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,012

Rep: Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731
Linux defaults to interposing earlier definitions during loading, which probably gives you an easy solution. I don't know enough of the exact rules, so if I were doing it myself, I would need to experiment to see what works.

The basic idea would be to include dummy functions in the .so file that have the same name as the target functions in the main executable but do nothing.

At load time, I think the loader will interpose the functions from the main image overriding the dummy versions in the .so.

In porting code from Windows (which does not do this) to Linux, I had several functions in the main image duplicating names of intentionally different functions in DLLs. In Windows it all works fine because nothing is exported nor imported without effort to make it exported and imported. In Linux, such things are exported, imported and interposed all by default and it took lots of effort to get those symbols to be final at link time so they couldn't be interposed. You have the opposite issue, which I think the Linux default behavior makes easier.
 
Old 06-17-2009, 11:11 PM   #4
rriggs
Member
 
Registered: Mar 2009
Location: Colorado, US
Distribution: Fedora 13, Fedora 14, RHEL6 Beta
Posts: 46

Rep: Reputation: 17
Mara's suggestion is the one I recommend. It is best to be explicit about a library calling into an application and the pattern for doing that is Register a Callback Function.

In C++, the easiest way to do that is to have the library define an interface (ABC) that is implemented by a class in the application. The application implements a class which implements the interface. The app registers an object of this type with the library. And the library just calls a method on the registered object.
 
Old 06-25-2009, 04:45 AM   #5
lennyk
LQ Newbie
 
Registered: Jan 2009
Posts: 18

Original Poster
Rep: Reputation: 0
Thanks! very helpful advice!
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help linking shared library g++ 4.1.2 x86_64 UlamTheLucky Programming 5 09-23-2007 04:55 PM
LINUX - linking archive (static library) with shared (dynamic) library gurkama Programming 5 03-04-2007 11:11 PM
strange linking error -- can not find shared library George2 Programming 1 07-10-2006 11:24 AM
Commandline legth problem linking a shared library DavePrince Linux - Software 0 07-20-2005 08:19 AM
Linking a platform independent shared library rozeboom Programming 3 11-18-2003 02:10 PM


All times are GMT -5. The time now is 04:51 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
Open Source Consulting | Domain Registration