LinuxQuestions.org
Review your favorite Linux distribution.
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 06-06-2007, 01:09 PM   #1
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Rep: Reputation: 22
Retrieving location of shared object file (Linux, C++) ?


Hi there,

first let me explain how this all works ...

There is a host (main process) which can load several independant shared objects (*.so) from various locations, as sort of extensions to the main process, to support different kind of other functionalities.
So far so good. Those shared objects are develloped individually from the host, and they basically can be located anywhere (i.e. the user can decide where to put them on the hard disk). Ok.
The shared objects occasionally will need to load stuff from disk, so sometimes they need to be able to retrieve their current location in the file system.

The question now is how would I go about this ?
I need a way to ask linux for the shared object's current path (c++) from inside the according *.so .

Anyone able to give me a clue ?

Thanks
f
 
Old 06-06-2007, 06:55 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Do you have control over the plug-in system? If so, add an argument to the plug-in entry point to denote a path. Try this:
Code:
//main.cpp

#include <string>
#include <iostream> //standard output
#include <dlfcn.h>  //'dlopen', etc.
#include <libgen.h> //'dirname'


typedef void(*load_function)(const char*);


int main(int argc, char *argv[])
{
	if (argc < 2)
	{
	std::cerr << argv[0] << " [plug-in name(s)]\n";
	return 1;
	}

	for (int I = 1; I < argc; I++)
	{
	std::string location = argv[ I ];
	dirname(&location[0]);


	void *handle = dlopen(argv[ I ], RTLD_NOW);

	if (!handle)
	 {
	std::cerr << argv[0] << ": " << dlerror() << "\n";
	continue;
	 }

	std::cerr << argv[0] << ": loading " << argv[ I ] << "\n";


	load_function function = (load_function) dlsym(handle, "load_plugin");

	if (!function)
	 {
	std::cerr << argv[0] << ": " << dlerror() << "\n";
	continue;
	 }

	(*function)(location.c_str());
	}

	return 0;
}
Code:
//testlib.cpp

#include <iostream> //standard output


extern "C" {
void load_plugin(const char*);
}


void load_plugin(const char *pPath)
{ std::cout << "libtest.so is located at: " << pPath << "\n"; }
Code:
shell~> g++ main.cpp -ldl -o testload
shell~> g++ -shared -fPIC testlib.cpp -o libtest.so
shell~> testload ./libtest.so
Try moving 'libtest.so' around and calling 'testload' with its current location. The shared lib can check cwd to turn a relative path into an absolute one. I've got a very simple tool to do that if you want it, though. http://sourceforge.net/projects/pmath-ta0kira/
ta0kira

Last edited by ta0kira; 06-07-2007 at 12:14 PM.
 
Old 06-07-2007, 10:06 AM   #3
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Original Poster
Rep: Reputation: 22
Thanks a lot for the fast reply

I already feared that I had to change the host interaction to accomplish this, if there is not a linux specific function which would return the location of the shared object file by itself.

This solution will be OK for now though

Thanks again,
f
 
Old 06-07-2007, 12:14 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The thing about loading shared code is that it becomes just another function for the program it's loaded from. Once you load a function, it becomes a part of the program that loaded it; it never runs as a library, independent of the using program. That makes it as if it were just another global function, and all context of where it came from is lost upon loading it. The only way I know how to let a library know where it is is to tell it where you found it. This is also true for programs; they don't know where they are located except as inferred by 'argv[0]' compared to 'cwd', but even that won't work when the program is found in PATH. You might want to make your libs use an env variable that tells where it can find supporting files, or hard-code a path in.
ta0kira
 
Old 06-08-2007, 06:18 AM   #5
fantas
Member
 
Registered: Jun 2007
Location: Bavaria
Distribution: slackware, xubuntu
Posts: 143

Original Poster
Rep: Reputation: 22
I now added a function to the host interface that can be called fom the plugin, querying the host for the plugin location.
No need to hardcode and still be able to call this dynamically.

TBH, maybe I was just a bit spoiled by the way WIN32 handles this, as there dlls can be queried for their filename.
Anyway ... this works as well

Cheers,
f
 
  


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
error while loading shared libraries: libstdc++.so.5: cannot open shared object file: Franziss Linux - Newbie 10 06-28-2010 05:47 AM
error while loading shared libraries: libgvc.so.3: cannot open shared object file coolrock Slackware 6 01-17-2007 05:10 PM
error while loading shared libraries: libdb-4.1.so: cannot open shared object file putquery8581 Linux - Software 1 10-01-2004 07:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration