How can I "register" my libfoo.so library to associate it with `-lfoo`?

  • How can I “register” my library foo.ccompiled in libfoo.soto link it to -lfoo? Does its path add to LD_LIBRARY_PATH? By running sudo ldconfig?

  • For curiosity, with whom am I “registering” him? That is, which application should “know” what it -lfoomeans for it to gcc bar.c -lfoowork? Is this a bash environment? Is it gcc? Is this the core?

  • Is this for static libraries (e.g.. libfoo.a)?

+4
source share
1 answer

"" , (, /usr/local/lib /usr/lib), -L, -L:

$ gcc -L/home/username/foo -Wall -o test main.c -lfoo
        |                                        |
        |__ location of libfoo.so or libfoo.a    |__ link libfoo.so or libfoo.a

GCC , 'lib .so .a(.so , .a , ).

  • -L - ,

  • -L (-lfoo "link library libfoo.so" )

(, ​​ , , ). LD_LIBRARY_PATH, rpath ldcondig. , .

LD_LIBRARY_PATH

$ export LD_LIBRARY_PATH=/home/username/foo:$LD_LIBRARY_PATH

libfoo.so, . LD_LIBRARY_PATH .

rpath

$ gcc -L/home/username/foo -Wl,-rpath=/home/username/foo -Wall -o test main.c -lfoo

rpath LD_LIBRARY_PATH ( ), rpath "" .

ldconfig

ldconfig, , , . (, sudo) .

$ sudo cp /home/username/foo/libfoo.so /usr/local/lib
$ sudo chmod 0755 /usr/local/lib/libfoo.so

/usr/local/lib , .

$ sudo ldconfig
$ ldconfig -p | grep foo
libfoo.so (libc6) => /usr/local/lib/libfoo.so

.

$ gcc -Wall -o test main.c -lfoo

ldconfig, -L, LD_LIBRARY_PATH rpath .


GCC Linux

+9

Source: https://habr.com/ru/post/1666040/


All Articles