根据官方文档
-llibrary
-l library
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)The -l option is passed directly to the linker by GCC. Refer to your linker documentation for exact details. The general description below applies to the GNU linker.The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with -L.Static libraries are archives of object files, and have file names like liblibrary.a. Some targets also support shared libraries, which typically have names like liblibrary.so. If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the -static option is used.It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.
在使用-l
选项时,有以下几点需要注意:
-labc
和-l abd
都可以使用,但第二种方法仅限于支持posix的系统。- 使用
-L
可以指定链接库的搜索路径。 - 如果同时搜索到静态库、动态库,优先使用动态库。除非使用了
-static
选项。 - 链接器会按照指定的顺序加载依赖库或二进制文件,所以要确认被依赖项在源文件的后面。