GCC -l 选项添加链接库

链接器把多个二进制的目标文件 (object file) 链接成一个单独的可执行文件。在链接过程中,必须把符号 (变量名、函数名等标识符) 用对应的内存地址 (变量地址、函数地址等) 替代,以完成程序中多个模块的外部引用。

链接器也必须将程序中所用到的所有 C 标准库函数加入其中。对于链接器而言,链接库不过是一个具有许多目标文件的集合,它们在一个文件中以方便处理。

当把程序链接到一个链接库时,只会链接程序所用到的函数的目标文件。在已编译的目标文件之外,如果创建自己的链接库,可以使用 ar 命令。

标准库的大部分函数通常放在文件 libc.a 中 (后缀 .a 代表 achieve,译为获取),或者放在用于共享的动态链接文件 libc.so 中 (后缀 .so 代表 share object,译为共享对象)。

当使用 GCC 编译和链接程序时,GCC 默认会链接 libc.a 或者 libc.so,但是对于其他的库 (例如非标准库、第三方库等),就需要手动添加。

标准头文件 <math.h> 对应的数学库默认不会被链接,如果没有手动将它添加进来,就会发生函数未定义错误。

GCC 的 -l 选项可以让我们手动添加链接库。数学程序 main.c,并使用到了 cos() 函数,它位于 <math.h> 头文件。

strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 4
-rw-rw-r-- 1 strong strong 284 Sep 29 10:39 main.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ cat main.c
#include <stdio.h>      /* printf */
#include <math.h>       /* cos */

#define PI 3.14159265

int main ()
{
    double angle, value;

    angle = 60.0;
    value = cos ( angle * PI / 180.0 );
    printf ("The cosine of %f degrees is %f.\n", angle, value );

    return 0;
}

strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ gcc main.c -o main
/tmp/cc3IKLgc.o: In function `main':
main.c:(.text+0x33): undefined reference to `cos'
collect2: error: ld returned 1 exit status
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ gcc main.c -o main -lm
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 16
-rwxrwxr-x 1 strong strong 8656 Sep 29 10:41 main
-rw-rw-r-- 1 strong strong  284 Sep 29 10:39 main.c
strong@foreverstrong:~/Desktop/makefile_work$

ldd main

strong@foreverstrong:~/Desktop/makefile_work$ ldd main
        .1 =>  (0x00007ffcaa7f1000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb14114d000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb140d83000)
        /lib64/.2 (0x00007fb141456000)
strong@foreverstrong:~/Desktop/makefile_work$

file main

strong@foreverstrong:~/Desktop/makefile_work$ file main
main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/.2, for GNU/Linux 2.6.32, BuildID[sha1]=fd1f5d19fc754da56e091d5d0230e3d7809c344a, not stripped
strong@foreverstrong:~/Desktop/makefile_work$

为了编译 main.c,必须使用 -l 选项,以链接数学库:

$ gcc main.c -o main.out -lm

数学库的文件名是 libm.a。前缀 lib 和后缀 .a 是标准的,m 是基本名称,GCC 会在 -l 选项后紧跟着的基本名称的基础上自动添加这些前缀、后缀。
数学库的文件名是 libm.so。前缀 lib 和后缀 .so 是标准的,m 是基本名称,GCC 会在 -l 选项后紧跟着的基本名称的基础上自动添加这些前缀、后缀。

locate libm.a

strong@foreverstrong:~$ locate libm.a
/usr/lib/x86_64-linux-gnu/libm.a
strong@foreverstrong:~$

locate libm.so

strong@foreverstrong:~$ locate libm.so
/lib/i386-linux-gnu/libm.so.6
/lib/x86_64-linux-gnu/libm.so.6
/lib32/libm.so.6
/snap/core/7396/lib/i386-linux-gnu/libm.so.6
/snap/core/7396/lib/x86_64-linux-gnu/libm.so.6
/snap/core/7713/lib/i386-linux-gnu/libm.so.6
/snap/core/7713/lib/x86_64-linux-gnu/libm.so.6
/snap/core18/1098/lib/i386-linux-gnu/libm.so.6
/snap/core18/1098/lib/x86_64-linux-gnu/libm.so.6
/snap/core18/1144/lib/i386-linux-gnu/libm.so.6
/snap/core18/1144/lib/x86_64-linux-gnu/libm.so.6
/usr/lib/x86_64-linux-gnu/libm.so
strong@foreverstrong:~$

1. 链接库路径

通常 GCC 会自动在标准库目录中搜索文件,such as /usr/local/lib and /usr/lib。如果想链接其它目录中的库,就需要特别指明。

(1) gcc main.c -o main /lib/x86_64-linux-gnu/libm.so.6 把链接库作为一般的目标文件,为 GCC 指定链接库的完整路径与文件名。
如果链接库名为 libm.so,并且位于 /usr/lib 目录,下面的命令会让 GCC 编译 main.c,然后将 libm.so 链接到 main.o

strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 4
-rw-rw-r-- 1 strong strong 284 Sep 29 10:39 main.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ gcc main.c -o main
/tmp/ccgbDbKT.o: In function `main':
main.c:(.text+0x33): undefined reference to `cos'
collect2: error: ld returned 1 exit status
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ gcc main.c -o main /lib/x86_64-linux-gnu/libm.so.6
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 16
-rwxrwxr-x 1 strong strong 8656 Sep 29 11:46 main
-rw-rw-r-- 1 strong strong  284 Sep 29 10:39 main.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ./main
The cosine of 60.000000 degrees is 0.500000.
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ file main
main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/.2, for GNU/Linux 2.6.32, BuildID[sha1]=fd1f5d19fc754da56e091d5d0230e3d7809c344a, not stripped
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ldd main
        .1 =>  (0x00007ffc995a5000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f13b531b000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f13b4f51000)
        /lib64/.2 (0x00007f13b5624000)
strong@foreverstrong:~/Desktop/makefile_work$

(2) gcc -v main.c -o main -lm

strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 4
-rw-rw-r-- 1 strong strong 284 Sep 29 10:39 main.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ gcc -v main.c -o main -lm
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.9' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/5/cc1 -quiet -v -imultiarch x86_64-linux-gnu main.c -quiet -dumpbase main.c -mtune=generic -march=x86-64 -auxbase main -version -fstack-protector-strong -Wformat -Wformat-security -o /tmp/ccc5aT93.s
GNU C11 (Ubuntu 5.4.0-6ubuntu1~16.04.9) version 5.4.0 20160609 (x86_64-linux-gnu)
        compiled by GNU C version 5.4.0 20160609, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/5/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C11 (Ubuntu 5.4.0-6ubuntu1~16.04.9) version 5.4.0 20160609 (x86_64-linux-gnu)
        compiled by GNU C version 5.4.0 20160609, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: d079eab342c322d6be59e8628e10ae67
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-mtune=generic' '-march=x86-64'
 as -v --64 -o /tmp/cchbbZAz.o /tmp/ccc5aT93.s
GNU assembler version 2.26.1 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.26.1
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/5/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/5/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper -plugin-opt=-fresolution=/tmp/ccu0yM24.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/.2 -z relro -o main /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/5 -L/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/5/../../.. /tmp/cchbbZAz.o -lm -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/5/crtend.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crtn.o
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 16
-rwxrwxr-x 1 strong strong 8656 Sep 29 11:59 main
-rw-rw-r-- 1 strong strong  284 Sep 29 10:39 main.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ./main
The cosine of 60.000000 degrees is 0.500000.
strong@foreverstrong:~/Desktop/makefile_work$

(3) gcc main.c -o main -static -lm 强制链接静态库
Static linking can be forced with the -static option to gcc to avoid the use of shared libraries.

strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 4
-rw-rw-r-- 1 strong strong 284 Sep 29 10:39 main.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ gcc main.c -o main
/tmp/ccBjHTPD.o: In function `main':
main.c:(.text+0x33): undefined reference to `cos'
collect2: error: ld returned 1 exit status
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ gcc main.c -o main -static -lm
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 992
-rwxrwxr-x 1 strong strong 1007688 Sep 29 11:48 main
-rw-rw-r-- 1 strong strong     284 Sep 29 10:39 main.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ./main
The cosine of 60.000000 degrees is 0.500000.
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ file main
main: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=bf105ee3ab9eff7b42b7fb63549336c22d156cbb, not stripped
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ldd main
        not a dynamic executable
strong@foreverstrong:~/Desktop/makefile_work$

(4) gcc -v main.c -o main -static -lm Try running the compilation in verbose mode (-v) to study the library-paths (-L) and libraries (-l) used in your system (尝试以详细模式 (-v) 运行编译,以研究系统中使用的库路径 (-L) 和库 (-l))。

strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 4
-rw-rw-r-- 1 strong strong 284 Sep 29 10:39 main.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ gcc -v main.c -o main -static -lm
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.9' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-static' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/5/cc1 -quiet -v -imultiarch x86_64-linux-gnu main.c -quiet -dumpbase main.c -mtune=generic -march=x86-64 -auxbase main -version -fstack-protector-strong -Wformat -Wformat-security -o /tmp/ccIFPNhM.s
GNU C11 (Ubuntu 5.4.0-6ubuntu1~16.04.9) version 5.4.0 20160609 (x86_64-linux-gnu)
        compiled by GNU C version 5.4.0 20160609, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/5/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C11 (Ubuntu 5.4.0-6ubuntu1~16.04.9) version 5.4.0 20160609 (x86_64-linux-gnu)
        compiled by GNU C version 5.4.0 20160609, GMP version 6.1.0, MPFR version 3.1.4, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: d079eab342c322d6be59e8628e10ae67
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-static' '-mtune=generic' '-march=x86-64'
 as -v --64 -o /tmp/ccR0dOde.o /tmp/ccIFPNhM.s
GNU assembler version 2.26.1 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.26.1
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/5/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' 'main' '-static' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/5/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/5/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper -plugin-opt=-fresolution=/tmp/cc62SxaG.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_eh -plugin-opt=-pass-through=-lc --sysroot=/ --build-id -m elf_x86_64 --hash-style=gnu --as-needed -static -z relro -o main /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/5/crtbeginT.o -L/usr/lib/gcc/x86_64-linux-gnu/5 -L/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/5/../../.. /tmp/ccR0dOde.o -lm --start-group -lgcc -lgcc_eh -lc --end-group /usr/lib/gcc/x86_64-linux-gnu/5/crtend.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crtn.o
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ls -l
total 992
-rwxrwxr-x 1 strong strong 1007688 Sep 29 11:53 main
-rw-rw-r-- 1 strong strong     284 Sep 29 10:39 main.c
strong@foreverstrong:~/Desktop/makefile_work$
strong@foreverstrong:~/Desktop/makefile_work$ ./main
The cosine of 60.000000 degrees is 0.500000.
strong@foreverstrong:~/Desktop/makefile_work$

(5) 使用 -L 选项,为 GCC 增加一个搜索链接库的目录。可以使用多个 -L 选项,或者在一个 -L 选项内使用冒号分割的路径列表。
(6) 把包括所需链接库的目录加到环境变量 LIBRARYPATH 中。