2

I try to create shared library and compile my main.c with this library

I follow this web site : http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

I give these commands :

gcc -fPIC -c *.c
gcc -shared -Wl,-rpath,/opt/lib -Wl,-soname,libctest.so.1 -o libctest.so.1.0   *.o
sudo mv libctest.so.1.0 /opt/lib
sudo ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so
sudo ln -sf /opt/lib/libctest.so.1.0 /opt/lib/libctest.so.1
gcc -Wall -L/opt/lib main.c -lctest -o prog

Commands gave no error. When I execute binary file ./prog it gives ./prog: error while loading shared libraries: libctest.so.1: cannot open shared object file: No such file or directory

but libctest.so.1 is in /opt/lib

lrwxrwxrwx 1 root  root    24 Aug 18 17:06 libctest.so -> /opt/lib/libctest.so.1.0
lrwxrwxrwx 1 root  root    24 Aug 18 17:06 libctest.so.1 -> /opt/lib/libctest.so.1.0
-rwxr-xr-x 1 user  user  7064 Aug 18 17:05 libctest.so.1.0

Also ldd prog is

linux-vdso.so.1 (0x00007ffe0f559000)
libctest.so.1 => not found
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcd27fc6000)
/lib64/ld-linux-x86-64.so.2 (0x00007fcd28371000)

so what is wrong ?

I used debian 8.5 and gcc 4.9.2

5
  • 2
    You could use the linker -rpath option to specify additional places where the executable is to search for libraries, rather than moving them around.... Commented Aug 18, 2016 at 14:44
  • Thank you @gilez rpath solved my problem. If you post ypur comment as an answer, I will accept. Commented Aug 23, 2016 at 15:05
  • I expanded my comment into an answer, but as I said, I'm a bit confused now. Are you still having problems, or is this all working? Commented Aug 24, 2016 at 17:58
  • I edited my answer a bit, hoping I'm not actually confused any more :-) Commented Aug 24, 2016 at 18:10
  • Thanks again @gilez. You solved my problem. Commented Aug 24, 2016 at 23:20

3 Answers 3

5

GCC's ld command has an --rpath option that may solve your problems:

-rpath=dir
       Add a directory to the runtime library search path.

You should add the location of your compiled library to GCC's command line when you compile prog, via the -wl option:

-Wl,option
       Pass option as an option to the linker. If option contains commas,
       it is split into multiple options at the commas.

So your search path already includes /opt/lib because of the original creation of the library:

-Wl,-rpath,/opt/lib

For the second compile, add the location of libctest.so.1.0 as another rpath, and it should be found without your needing to move files around:

gcc -Wall -L/opt/lib main.c -lctest -Wl,-rpath,/you/dir/name -o prog

I think your original effort is failing as the linker has included a hard path to your original outout dir, and then you moved the library from under it.

Sign up to request clarification or add additional context in comments.

Comments

2

Try adding /opt/lib to LD_LIBRARY_PATH as below;

LD_LIBRARY_PATH=/opt/lib 

1 Comment

Setting LD_LIBRARY_PATH has distinct drawback of "polluting" all child processes. When you are in control of the final link, a much better solution is to add -Wl,-rpath=/opt/lib to the link command instead.
1

I have a same proplem with iccxml

iccToXml profile.icc profile.xml

When i convert *.icc to *.xml by above code, i recived same message: cannot open shared object file: No such file or directory, Detail:

iccToXml: error while loading shared libraries: libIccXML.so.2: cannot open shared object file: No such file or directory

I resolved by use this command before convert

export LD_LIBRARY_PATH="/usr/local/lib"

You can replace "/usr/local/lib" by path of application file that you want.

Wish that is should helpfull for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.