LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-24-2009, 09:17 PM   #1
posop
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Rep: Reputation: 0
g++ dynamically linked library not being found


Hi this is my first post.

this question is about g++ version 4.3.3 and dynamically linked libraries.

if compile and run my program using:

Code:
g++ -o hellobeatles hellobeatles.o ../johnpaul/libjohnpaul.a ../georgeringo/libgeorgeringo.so
my executable runs just fine. ie ./hellobeatles outputs

Code:
$ ./hellobeatles 
John, Paul, George, and Ringo
but if i run it using:

Code:
g++ -o hellobeatles hellobeatles.o -L../johnpaul -L../gerogeringo -ljohnpaul -lgeorgeringo
I get no errors & i recieve an executable but when i run it:

Code:
./hellobeatles returns:

./hellobeatles: error while loading shared libraries:
 libgeorgeringo.so: cannot open shared object file: 
No such file or directory
Here is my output for ldd hellobeatles:

For the executable that worked, ldd outputs:
Code:
$ ldd hellobeatles
	linux-gate.so.1 =>  (0xb7f00000)
	../georgeringo/libgeorgeringo.so (0xb7efb000)
	libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb7dfd000)
	libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xb7dd6000)
	libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7dc7000)
	libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7c64000)
	/lib/ld-linux.so.2 (0xb7f01000)
The problem here is it is an absolute path. I can never move my executable without recreating the folder structure around it and move libgeorgeringo.so to the same relative location.


Here's what i want but the executable that doesn't work:

Code:
$ ldd hellobeatles
	linux-gate.so.1 =>  (0xb8073000)
	libgeorgeringo.so => not found
	libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0xb7f73000)
	libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0xb7f4c000)
	libgcc_s.so.1 => /lib/libgcc_s.so.1 (0xb7f3d000)
	libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7dda000)
	/lib/ld-linux.so.2 (0xb8074000)


So I am not finding my libgeorgeringo.so library. This second method is what I prefer so i don't have to have my libFILENAME.so in a locked location relative to the executable.

(1) I tried adding the folder that holds the library to $PATH.
(2) I tried copying libgeorgeringo.so to /usr/bin
(3) I tried copying libgeorgeringo.so to the same folder as the executable
...
still no joy.

Why is g++ correctly finding the file if i specify it by name? But not when i try to have it dynamicaly linked. ie use -l option in g++ can anyone help me understand how to do this?

Please specify:
where to put my libFILENAME.so & how to have my executable program find the library. thank you so much for your help.
 
Old 10-25-2009, 05:30 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
There is a difference between the path used to access the library definitions at link time, and the path used to dynamically load the library at runtime.

The -L flag you give to gcc gives it a path to search for static libraries and for dynamic libraries for use by the linker, but this does not affect where the executable will search for dynamic libraries (typically only /lib and /usr/lib, not /usr/bin).

There are several ways of providing a runtime library path. An older method was to use the environment variable LD_LIBRARY_PATH. Now, if you want to provide global access to the dynamic libraries, you would add their path to one of the /etc/ld.so.conf.d/ files, and run ldconfig (see 'man ldconfig'). Or if you just want it for a single application you can incorporate it into the executable by using the linker flag '-R /path/to/library'.

Last edited by neonsignal; 10-25-2009 at 05:33 AM.
 
Old 10-25-2009, 10:23 AM   #3
posop
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
neonsignal, Thank you this is exactly the information I was after. as you can tell I'm no guru, but you're helping me get there.

Problem solved!
 
Old 11-01-2009, 08:14 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 04-14-2010, 02:38 PM   #5
posop
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
problem using -R flag

Ok I had been adding the .so files to my /usr/local/lib, but I want to know how to use the '-R /path/to/library' type execution.

Here's what I've tried so far:
Code:
./hellobeatles -R ../georgeringo/libgeorgeringo.so
no joy,
Code:
./hellobeatles '-R ../georgeringo/libgeorgeringo.so'
no good,
Code:
./hellobeatles '-R ../georgeringo/'
none of these attempts run the file each one outputs:
Code:
./hellobeatles: error while loading shared libraries: libgeorgeringo.so: cannot open shared object file: No such file or directory
So obviously I don't know how to call the executable with a -R extension. could anyone point me in the right direction?

Thank you for your help.
 
Old 04-14-2010, 03:13 PM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by posop View Post
I had been adding the .so files to my /usr/local/lib, but I want to know how to use the '-R /path/to/library' type execution.
That isn't an execution (load) time option. That is a link time option to modify the load time behavior.

To do something like that at load time, you must modify the LD_LIBRARY_PATH environment variable.
 
Old 04-14-2010, 03:20 PM   #7
posop
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
ok thank you.
 
  


Reply

Tags
dynamic, g++


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
dynamically list all symbols from a shared library famsinyi Programming 2 09-09-2009 06:21 PM
Dynamically linked libraries.. vishalbutte Programming 6 04-17-2006 08:54 PM
library linked twice okhan Programming 1 08-12-2005 10:33 PM
howto compile bin with my library using all-static and shared linked standart library stpg Programming 4 06-29-2004 04:20 AM
How do you load dynamically a library from C? Hano Programming 4 06-07-2002 03:04 PM

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

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