-l -L and related knowledge points

These two commands are basically the same, and they both serve the library loading at link time (dynamic library.so static library.a)

But their roles are very different: -L is the same as -I, used to specify the search path, -l is used to specify the name of the library that needs to be linked when linking


Generate static library: gcc -o func1.o func1,c

                    gcc -o func2.o func2.c

                    ar -rcs libfunc.a func1.o func2.o

Generate dynamic library: gcc -o func1.o func1.c

                    gcc -o func2.o func2.c

                    gcc -shared -fPIC -o libfunc.so func1.o func2.o


-l : Look only in /lib /usr/lib and /usr/local/lib directories.

                For example: gcc -o 1 1.c -lfunc (assuming the libfunc.a or libfunc.so file is in any of the three directories above)

-L : Specifies the search directory for library files

                For example: gcc -o 1 1.c -L./ -lfunc (assuming the libfunc.a or libfunc.so file is in the current directory)


tips:

1. If the dynamic library and the static library have the same name and can be searched, then if the -static option is not applicable, the dynamic library will be used first by default

        Force the use of a static library: gcc -o 1 1.c -static -lfunc

2. Why does the name of the library have to start with lib? Why is the -l option not followed by the full name of the library file?

3.

For the dynamic library, the libtest.so library is only marked in the target program, and will be dynamically loaded when the program is run, so where to load it? The loading directory will be specified by /etc/ld.so.conf, generally the default is /lib, /usr/lib, so to make the dynamic library load smoothly, you can copy the library files to the above two directories, or Set export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/XXX/YYY, followed by the directory of your own dynamic library, or modify the /etc/ld.so.conf file, add the path of the library to the end of the file, and execute ldconfig to refresh. In this way, all library files under the added directory are visible.

In addition, there is another file that you need to know about /etc/ld.so.cache, which stores the commonly used dynamic function libraries, and will load them into the memory first, because the access speed of the memory is much faster than the access speed of the hard disk, so you can Improve the speed of software loading dynamic function library.

Reference: https://www.cnblogs.com/zhangxuan/p/5382946.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324833276&siteId=291194637