C with a shared library without setting LD_LIBRARY_PATH

I read "Introduction to GCC" and he says the package has both .a and .so. gcc prefers a shared library. By default, the loader only searches for shared libraries in a predefined set of system directories, such as /usr/local/liband /usr/lib. If the library is not in one of these directories, it must be added to the download path, or you need to use the option -staticto force it to use the .a library. However, I tried the following:

vim hello.c:

#include <gmp.h>
#include <stdio.h>

int main() {
        mpz_t x;
        mpz_init(x);
        return 0;
}

gcc hello.c -I/opt/include -L/opt/lib -lgmp  (my gmp library is in opt)
./a.out

And it works. The book says that it should have the following error:

./a.out: error while loading shared libraries:
libgdbm.so.3: cannot open shared object file:
No such file or directory

(well, the book uses the GDBM example, but I used GMP, but that doesn't matter?)

LD_LIBRARY_PATH=/opt/lib, , , -static, a.out .

, , , ? , , , .

+5
2

:

linux-gate.so.1 => (0xb7746000)
libgmp.so.10 => /usr/lib/i386-linux-gnu/libgmp.so.10 (0xb76c5000)
libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0xb7520000)
/lib/ld-linux.so.2 (0xb7747000) 

, lib /usr/lib.

, lib /opt/lib .

mv /opt/lib/libgmp.so /opt/lib/libgmp-test.so
gcc hello.c -I/opt/include -L/opt/lib -lgmp-test

. , ldd a.out , .

+3

. - ( Linux).

  • , , /etc/ld.so.conf.d/*.
  • , /etc/ld.so.conf
  • ldconfig

- dlopen().

0

All Articles