0

I'm playing with shared library linking and loading procedures, and trying to learn about different ways to tackle the loading error:

error while loading shared libraries: ... : cannot open shared object file: No such file or directory

I managed to solve it by using LD_LIBRARY_PATH and by using rpath. I now try to comprehend the usage of ldconfig and how it can solve it, too [some of my web sources: 1, 2, 3 ]

So, I've got the following source files: test.c func1.c func2.c. Here's what I do:

  1. Compiling them into PIC .obj files:

$ gcc -Wall -fPIC -c *.c

  1. Creating a shared library from the object files, naming it 'libshared.so', and using 'soname':

$ gcc -shared -Wl,-soname,libshared.so -o libshared.so *.o

  1. Linking with a shared library by telling GCC where to find the shared library (the ... is just an abbreviation for my personal shared library path):

$ gcc -L/home/.../my_shared_library -Wall -o mytest test.c -lshared

  1. Now, I try running it, and get the loading error:

./mytest: error while loading shared libraries: libshared.so: cannot open shared object file: No such file or directory

  1. So I try telling the loader to search my private directory for the shared library file:

ldconfig -n home/.../my_shared_library

(I also tried it with: ldconfig -n .-L when my working directory is home/.../my_shared_library)

and tried running it once again, but I still get the same error. What am I doing wrong?

OfirD
  • 9,442
  • 5
  • 47
  • 90

2 Answers2

2

Use GNU Libtool to simplify the process and avoid such errors. Also use -fPIC option when you build your library.

Peter Silon
  • 457
  • 2
  • 11
  • Thanks for this tool, I wasn't familiar with it. However, I want to use 'direct' shell commanding, ldconfig specifically, and not hide the complexity of using shared libraries. – OfirD Feb 29 '16 at 16:23
0

Read Program Library HowTo & Drepper's paper: How to Write Shared Libraries

You probably want to set the rpath. See this question.

You might load your plugin at runtime with dlopen(3). Notice that shared object paths without any / are handled specially.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • the HowTo manual is one of my learning sources, and I linked to a specific page that discusses my question content. I'm familiar with rpath, as I wrote, but I want to make things work using ldconfig (How to Write Shared Libraries also doesn't help regarding this). – OfirD Feb 29 '16 at 16:19
  • But you don't set `rpath`. I would put once `/usr/local/lib/` in `/etc/ld.so.conf`, put the library under `/usr/local/lib/` and run `ldconfig` – Basile Starynkevitch Feb 29 '16 at 17:33
  • Well, my understanding is that `ldconfig -n ` should make it possible for the loader to search the specified directory after my shared library, without the need to put it into /usr/local/lib, and without the need to use rpath. This understanding stems from some reading, and here's one example: "You can use gcc's -L flag in simple cases. You can use the ``rpath'...'" (from here [link] http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html) – OfirD Feb 29 '16 at 17:45