Operating System - Linux
1826578 Members
4002 Online
109695 Solutions
New Discussion

Re: Linking shared libraries

 
Preetam
Occasional Advisor

Linking shared libraries

I have been trying to link a shared library to my program. There are no errors given by any of the commands but while executing the program i get an error. Below are the steps i followed and the results
[preetam@cyclone]junkc> ls
main.c test.c
[preetam@cyclone]junkc> gcc -c -fPIC test.c
[preetam@cyclone]junkc> gcc -shared -o libtest.so test.o
[preetam@cyclone]junkc> gcc -c main.c
[preetam@cyclone]junkc> set LD_LIBRARY_PATH=(${LD_LIBRARY_PATH}:/h/preetam/junkc)
[preetam@cyclone]junkc> echo $LD_LIBRARY_PATH
/usr/lib:/usr/local/lib:/h/preetmo/junkc
[preetam@cyclone]junkc> gcc -o mainprog main.o -L. -ltest
[preetam@cyclone]junkc> mainprog
ld.so.1: mainprog: fatal: libtest.so: open failed: No such file or directory
Killed

Please let me know what is wrong with it??
10 REPLIES 10
spex
Honored Contributor

Re: Linking shared libraries

V. Nyga
Honored Contributor

Re: Linking shared libraries

Hi,

can you check with 'ldd mainprog' and 'chatr mainprog' where the program expects the shared libraries?

Then create a link or add the directory to your env PATH.
Maybe you have to set your LD_LIBRARY_PATH in the program?

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
Preetam
Occasional Advisor

Re: Linking shared libraries

Hi Volkmar,
I used ldd mainprog and below is the output
[preetam@cyclone]junkc> ldd mainprog
libtest.so => (file not found)
libc.so.1 => /usr/lib/libc.so.1
libdl.so.1 => /usr/lib/libdl.so.1
/usr/platform/SUNW,Ultra-4/lib/libc_psr.so.1

For chatr command was not found.

I have updated the LD_LIBRARY_PATH as mentioned in the previous post to my current directory but it still doesnt work. I have also tried giving the path to gcc in the command line as follows:

[preetam@cyclone]junkc> gcc -o mainprog shared.o -L. -ltest -Wl,-rpath,/h/preetam/libtest

but it still doesnt work.

I read somewhere that there is an alternative method to regenerate the library cache file by typing "ldconfig" in the installed directory. But my system does not have even that command!!! :(

I am using SunOS 5.8

Preetam.
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Linking shared libraries

Use:

gcc -o mainprog main.o -L. -L/h/preetmo/junkc -ltest

As far as i remember

LD_LIBRARY_PATH is not used during searching of libraries.
Its used during run-time.
Vibhor Kumar Agarwal
V. Nyga
Honored Contributor

Re: Linking shared libraries

Hi again,

I don't know how to use 'gcc', but maybe you have to give the complete path of 'libtest.so' when you declare it as a library.
Where have you stored it, in your home dir?

Maybe you also can create a link in /usr/lib to 'libtest.so', also I think it's not the 'clean' way to do it.

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
Preetam
Occasional Advisor

Re: Linking shared libraries

Hi vibhor,
I tried what you said. Same result!! does not work...

Volkmar I cant create a link in /usr/lib as it is on a different file system. I used command ln -f ~/junkc/libtest.so

Regards,
Preetam.
V. Nyga
Honored Contributor

Re: Linking shared libraries

Hi again,

for a short test - copy libtest.so into /usr/lib and see if it works.

V.
*** Say 'Thanks' with Kudos ***
Dennis Handly
Acclaimed Contributor

Re: Linking shared libraries

It seems your problems are on a Sun system since you mentioned "SunOS 5.8". Why are you asking these questions here??

On HP-UX, if you have exported the PWD in LD_LIBRARY_PATH it should just work. Not knowing if you are using a "real" shell or not, I don't think "set" exports the variable?? The scummy C shell requires setenv. A "real" shell requires export.
V. Nyga
Honored Contributor

Re: Linking shared libraries

Hi Dennis,

I think his 'set' worked, because in his 'echo' you can see the added path.

V.
*** Say 'Thanks' with Kudos ***
Dennis Handly
Acclaimed Contributor

Re: Linking shared libraries

>V. Nyga: I think his 'set' worked, because in his 'echo'

I first thought so too but that is not how shell variables work. You should use the env(1) command and see if the variable is exported. Using any command with a $variable will just have the shell expand it without exporting it.
$ csh # start scummy C shell
% set xxx=yyy
% echo $xxx # this command proves nothing
yyy
% env | fgrep xxx
% setenv xxx "$xxx"
% env | fgrep xxx # now part of the environment
xxx=yyy