LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 03-08-2006, 08:00 AM   #1
smrcis
LQ Newbie
 
Registered: Feb 2006
Distribution: Fedora Core 4
Posts: 5

Rep: Reputation: 0
LD_LIBRARY_PATH and dlopen


Hi, I have a small problem. Considering 3 files: prog, liba.so, b.so in the same directory.

prog - program, which dlopens b.so
b.so - library, which is linked to the liba.so
liba.so - some library in user directory

All goes ok when I run it like this:
$ LD_LIBRARY_PATH=. ./prog

But I'd like to use only
$ ./prog
So before calling
dlopen("./b.so", RTLD_LAZY)
I set LD_LIBRARY_PATH:
setenv("LD_LIBRARY_PATH", ".", 1) /or putenv("LD_LIBRARY=.")/

But it didn't help. I successfully checked, if environment variable is set well via system("echo $LD_LIBRARY_PATH"). I watched strace and I can see there, that dlopen doesn't try to find liba.so in current directory.

Any clue?
 
Old 03-08-2006, 12:23 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Try this (needs to be done each time you start up):
Code:
 > ldconfig -n .
I've run into the same problem; this adds all libs in . to /etc/ld.so.cache.
ta0kira

PS Or just 'ldconfig' after changing LD_LIBRARY_PATH.

Last edited by ta0kira; 03-08-2006 at 12:25 PM.
 
Old 03-08-2006, 03:58 PM   #3
smrcis
LQ Newbie
 
Registered: Feb 2006
Distribution: Fedora Core 4
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks,

but I want to change my source so that I don't need to run special commands (like ldconfig) or do specific settings (like LD_LIBRARY_PATH).

I know, I can run the program via some script which set the LD_LIBRARY_PATH, but I'm looking for solution, how to change the program source code.

Last edited by smrcis; 03-08-2006 at 04:13 PM.
 
Old 03-08-2006, 04:49 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
How to avoid needing LD_LIBRARY_PATH

Please look at this thread:
http://www.linuxquestions.org/questi...d.php?t=408083

or this link:
http://www.tldp.org/HOWTO/Program-Li...libraries.html

Cheers!
 
Old 03-08-2006, 05:46 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
paulsm4; Is this sort of what you are talking about (in the context of the binary using the lib)?:

1) remove calls to dlopen, etc.
2) #include a header file with the function declarations used from the shared lib
3) use the functions by name
4) -L the current path and -l the actual libs at link time

I'm not sure if that's what you were thinking of, but it just came to me when I looked through the LQ post you linked to. I suppose you would still need to use ldconfig or LD_LIBRARY_PATH, though.
ta0kira
 
Old 03-08-2006, 06:10 PM   #6
smrcis
LQ Newbie
 
Registered: Feb 2006
Distribution: Fedora Core 4
Posts: 5

Original Poster
Rep: Reputation: 0
Avoiding LD_LIBRARY_PATH

paulsm4: Thanks, now it works exactly like I need. I guessed that it is somehow possible to store path to libraries in target ELF, but not found it. I looked once more to tldp.org and see the solution.

Now I compile it like this:

Code:
$ g++ prog.cc -ldl -o prog
$ g++ -shared liba.cc -o liba.so
$ g++ -shared -la -L. -Wl,-rpath,. b.cc -o b.so
$ ./prog
Important is -Wl part of parameters.
Thanks
 
  


Reply



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
dlopen(), causing SIGBUS signal.... help rajsun Programming 8 12-11-2005 10:08 PM
Enemy-Territory: dlopen libGL.so.1 failed DaneM Linux - Games 2 11-12-2005 11:41 PM
Dlopen function - UNDEFINED REFERENCE mamthababu Programming 1 07-19-2005 10:20 AM
dlopen in multi-threading environnement yakotey Programming 2 06-30-2005 08:36 AM
Forcing dlopen to look at LD_LIBRARY_PATH again wiredj Programming 2 09-24-2004 06:55 PM

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

All times are GMT -5. The time now is 04:15 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