LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   g++ dynamically linked library not being found (http://www.linuxquestions.org/questions/programming-9/g-dynamically-linked-library-not-being-found-764276/)

posop 10-24-2009 09:17 PM

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.

neonsignal 10-25-2009 05:30 AM

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'.

posop 10-25-2009 10:23 AM

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!

pixellany 11-01-2009 08:14 AM

Moved: This thread is more suitable in <Programming> and has been moved accordingly to help your thread/question get the exposure it deserves.

posop 04-14-2010 02:38 PM

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.

johnsfine 04-14-2010 03:13 PM

Quote:

Originally Posted by posop (Post 3935576)
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.

posop 04-14-2010 03:20 PM

ok thank you.


All times are GMT -5. The time now is 12:49 PM.