说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C2-C_depth/advanced_libraries.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
2.9.5. C Libraries: Using, Compiling, and Linking
库(library)实现了一组可供其他程序使用的函数和定义。C 库由两部分组成:
A library implements a collection of functions and definitions that can be used by other programs. A C library consists of two parts:
- 库的应用程序编程接口(API),定义在一个或多个头文件(
.h文件)中,打算使用该库的 C 源文件必须包含这些头文件。头文件定义了库向其用户导出的内容。这些定义通常包括库函数的原型,也可能包括类型、常量或全局变量的声明。
- The application programming interface (API) to the library, which gets defined in one or more header files (
.hfiles) that must be included in C source code files that plan to use the library. The headers define what the library exports to its users. These definitions usually include library function prototypes, and they may also include type, constant, or global variable declarations.
- 库功能的实现,通常以预编译的二进制形式提供给程序,由
gcc链接(加入)到它生成的二进制可执行文件中。预编译的库代码可能是一个归档文件(libsomelib.a),其中包含若干.o文件,可以在编译时静态链接到可执行文件中;也可能是一个共享目标文件(libsomelib.so),可以在运行时动态链接到正在运行的程序中。
- The implementation of the library’s functionality, often made available to programs in a precompiled binary format that gets linked (added) into the binary executable created by
gcc. Precompiled library code might be in an archive file (libsomelib.a) containing several.ofiles that can be statically linked into the executable file at compile time. Alternatively, it may consist of a shared object file (libsomelib.so) that can be dynamically linked at runtime into a running program.
例如,C 字符串库实现了一组操作 C 字符串的函数。string.h 头文件定义了它的接口,所以任何想使用字符串库函数的程序都必须 #include <string.h>。C 字符串库的实现是更大的标准 C 库(libc)的一部分,gcc 编译器会自动把 libc 链接到它生成的每个可执行文件中。
For example, the C string library implements a set of functions to manipulate C strings. The
string.hheader file defines its interface, so any program that wants to use string library functions must#include <string.h>. The implementation of the C string library is part of the larger standard C library (libc) that thegcccompiler automatically links into every executable file it creates.
库的实现由一个或多个模块(.c 文件)组成,还可能包含库实现内部使用的头文件;内部头文件不属于库的 API,但它们是设计良好的模块化库代码的一部分。库的 C 源代码实现通常不会提供给库的用户,而是以预编译的二进制形式提供。这些二进制格式不是可执行程序(不能单独运行),但它们提供了可执行代码,可以在编译时由 gcc 链接(加入)到可执行文件中。
A library’s implementation consists of one or more modules (
.cfiles), and may additionally include header files that are internal to the library implementation; internal header files are not part of the library’s API but are part of well-designed, modular library code. Often the C source code implementation of a library is not exported to the user of the library. Instead, the library is made available in a precompiled binary form. These binary formats are not executable programs (they cannot be run on their own), but they provide executable code that can be linked into (added into) an executable file bygccat compilation time.
C 程序员可以使用的库非常多。例如,POSIX 线程库(在第 10 章中讨论)使 C 程序可以多线程运行。C 程序员也可以实现和使用自己的库(在下一节中讨论)。大型 C 程序往往会用到许多 C 库,其中一些由 gcc 隐式链接,另一些则需要用 gcc 的 -l 命令行选项显式链接。
There are numerous libraries available for C programmers to use. For example, the POSIX thread library (discussed in Chapter 10) enables multithreaded C programs. C programmers can also implement and use their own libraries (discussed in the next section). Large C programs tend to use many C libraries, some of which
gcclinks implicitly, whereas others require explicit linking with the-lcommand line option togcc.
译注
原文的「第 10 章」应为第 14 章(共享内存并行),链接本身指向的也是第 14 章。
标准 C 库通常不需要用 -l 选项显式链接,但其他库需要。库函数的文档通常会说明编译时是否需要显式链接该库。例如,POSIX 线程库(pthread)和 readline 库需要在 gcc 命令行中显式链接:
Standard C libraries normally do not need to be explicitly linked in with the
-loption, but other libraries do. The documentation for a library function often specifies whether the library needs to be explicitly linked in when compiling. For example, the POSIX threads library (pthread) and thereadlinelibrary require explicit linking on thegcccommand line:
1 | $ gcc -o myprog myprog.c -pthread -lreadline |
注意,链接 POSIX 线程库是个特例,不使用 -l 前缀。不过,大多数库都是在 gcc 命令行中用 -l 语法显式链接到可执行文件中的。还要注意,-l 参数中不应写出库文件的完整文件名:库文件的名字类似 libreadline.so 或 libreadline.a,但文件名中的 lib 前缀和 .so 或 .a 后缀都不写。实际的库文件名可能还包含版本号(例如 libreadline.so.8.0),这些版本号也不写在 -l 命令行选项中(-lreadline)。由于不强制用户指定(甚至不需要知道)要链接的库文件的确切名称和位置,gcc 可以自由地在用户的库路径中查找库的最新版本。这也让编译器在一个库同时有共享目标(.so)版本和归档(.a)版本时,可以选择动态链接。如果用户想静态链接库,可以在 gcc 命令行中显式指定静态链接。--static 选项是请求静态链接的一种方法:
Note that linking the POSIX thread library is a special case that does not include the
-lprefix. However, most libraries are explicitly linked into the executable using the-lsyntax on thegcccommand line. Also note that the full name of the library file should not be included in the-largument togcc; the library files are named something likelibreadline.soorlibreadline.a, but thelibprefix and.soor.asuffix of the filenames are not included. The actual library filename may also contain version numbers (e.g.libreadline.so.8.0), which are also not included in the-lcommand line option (-lreadline). By not forcing the user to specify (or even know) the exact name and location of the library files to link in,gccis free to find the most recent version of a library in a user’s library path. It also allows the compiler to choose to dynamically link when both a shared object (.so) and an archive (.a) version of a library are available. If users want to statically link libraries, then they can explicitly specify static linking in thegcccommand line. The--staticoption provides one method for requesting static linking:
现代更新(截至 2026-11)· 不再需要 -lpthread
glibc 2.34(2021 年 8 月)把原先位于 libpthread、libdl、libutil、libanl 中的全部功能并入了 libc,新程序不必再链接 -lpthread、-ldl 等;为了兼容,这些库保留了空的静态归档文件,旧的链接选项照样可用。编译多线程程序时写 -pthread 仍然是好习惯(它同时会设置必要的预处理宏)。GNU C Library 2.34 发布公告
1 | $ gcc -o myprog myprog.c --static -pthread -lreadline |
编译步骤
Compilation Steps
刻画 C 程序的编译步骤,有助于说明库代码是如何被链接到可执行二进制文件中的。我们先介绍编译步骤,再(结合例子)讨论编译使用库的程序时可能出现的各类错误。
Characterizing C’s program compilation steps will help to illustrate how library code gets linked into an executable binary file. We first present the compilation steps and then discuss (with examples) different types of errors that can occur when compiling programs that use libraries.
C 编译器通过四个不同的步骤(外加一个在运行时进行的第五步),把 C 源文件(例如 myprog.c)翻译成可执行二进制文件(例如 a.out)。
The C compiler translates a C source file (e.g.,
myprog.c) into an executable binary file (e.g.,a.out) in four distinct steps (plus a fifth step that occurs at runtime).
首先运行预编译步骤(precompiler step),展开预处理指令:即 C 程序中出现的
#指令,例如#define和#include。这一步的编译错误包括预处理指令中的语法错误,或者gcc找不到与#include指令对应的头文件。要查看预编译步骤的中间结果,可以给gcc传入-E标志(输出可以重定向到一个文件,用文本编辑器查看):1
2
3$ gcc -E myprog.c
$ gcc -E myprog.c > out
$ vim out
The precompiler step runs first and expands preprocessor directives: the
#directives that appear in the C program, such as#defineand#include. Compilation errors at this step include syntax errors in preprocessor directives orgccnot finding header files associated with#includedirectives. To view the intermediate results of the precompiler step, pass the-Eflag togcc(the output can be redirected to a file that can be viewed by a text editor):
1
2
3 $ gcc -E myprog.c
$ gcc -E myprog.c > out
$ vim out
接下来运行编译步骤(compile step),它完成编译任务的主体部分:把 C 程序源代码(
myprog.c)翻译成特定于机器的汇编代码(myprog.s)。汇编代码是计算机可以执行的二进制机器码指令的人类可读形式。这一步的编译错误包括 C 语言语法错误、未定义符号警告,以及缺少定义和函数原型导致的错误。要查看编译步骤的中间结果,可以给gcc传入-S标志(这个选项会生成一个名为myprog.s的文本文件,内容是myprog.c翻译成的汇编代码,可以用文本编辑器查看):1
2$ gcc -S myprog.c
$ vim myprog.s
The compile step runs next and does the bulk of the compilation task. It translates the C program source code (
myprog.c) to machine-specific assembly code (myprog.s). Assembly code is a human-readable form of the binary machine code instructions that a computer can execute. Compilation errors at this step include C language syntax errors, undefined symbol warnings, and errors from missing definitions and function prototypes. To view the intermediate results of the compile step, pass the-Sflag togcc(this option creates a text file namedmyprog.swith the assembly translation ofmyprog.c, which can be viewed in a text editor):
1
2 $ gcc -S myprog.c
$ vim myprog.s
汇编步骤(assembly step)把汇编代码转换为可重定位的二进制目标代码(
myprog.o)。生成的目标文件包含机器码指令,但它不是一个可以单独运行的完整可执行程序。Unix 和 Linux 系统上的gcc编译器生成一种名为 ELF(Executable and Linkable Format,可执行与可链接格式)的特定格式的二进制文件。要在这一步之后停止编译,可以给gcc传入-c标志(这会生成一个名为myprog.o的文件)。二进制文件(例如a.out和.o文件)可以用objdump或类似的二进制文件查看工具来查看:1
2
3
4$ gcc -c myprog.c
# disassemble functions in myprog.o with objdump:
$ objdump -d myprog.o
The assembly step converts the assembly code into relocatable binary object code (
myprog.o). The resulting object file contains machine code instructions, but it is not a complete executable program that can run on its own. Thegcccompiler on Unix and Linux systems produces binary files in a specific format called ELF (Executable and Linkable Format). To stop compilation after this step, pass the-cflag togcc(this produces a file namedmyprog.o). Binary files (e.g.a.outand.ofiles) can be viewed usingobjdumpor similar tools for displaying binary files:
1
2
3
4 $ gcc -c myprog.c
# disassemble functions in myprog.o with objdump:
$ objdump -d myprog.o
最后运行链接编辑步骤(link editing step),它用可重定位二进制文件(
.o)和库(.a或.so)生成单个可执行文件(a.out)。在这一步中,链接器会核实.o文件中对名字(符号)的所有引用,都能在其他.o、.a或.so文件中找到。例如,链接器会在标准 C 库(libc.so)中找到printf函数。如果链接器找不到某个符号的定义,这一步就会失败,并报错说某个符号未定义。不带部分编译标志运行gcc,会执行全部四个步骤,把 C 源代码文件(myprog.c)编译成可以运行的可执行二进制文件(a.out):1
2
3
4
5$ gcc myprog.c
$ ./a.out
# disassemble functions in a.out with objdump:
$ objdump -d a.out如果二进制可执行文件(
a.out)静态链接了库代码(来自.a库文件),那么gcc会把.a文件中库函数的副本嵌入到生成的a.out文件中。应用程序对库函数的所有调用,都被绑定(bound)到a.out文件中库函数副本所在的位置。绑定是把一个名字与程序内存中的一个位置关联起来。例如,绑定对名为gofish的库函数的调用,就是把对这个函数名的使用替换为该函数在内存中的地址(后面的章节会更详细地讨论内存地址)。但如果
a.out是通过动态链接库(来自共享目标库.so文件)生成的,那么a.out中就不包含这些库的库函数代码副本,而是包含一些信息,说明运行a.out需要哪些动态链接库。这样的可执行文件在运行时还需要一个额外的链接步骤。
The link editing step runs last and creates a single executable file (
a.out) from relocatable binaries (.o) and libraries (.aor.so). In this step, the linker verifies that any references to names (symbols) in a.ofile are present in other.o,.a, or.sofiles. For example, the linker will find theprintffunction in the standard C library (libc.so). If the linker cannot find the definition of a symbol, this step fails with an error stating that a symbol is undefined. Runninggccwithout flags for partial compilation performs all four steps of compiling a C source code file (myprog.c) to an executable binary file (a.out) that can be run:
1
2
3
4
5 $ gcc myprog.c
$ ./a.out
# disassemble functions in a.out with objdump:
$ objdump -d a.outIf the binary executable file (
a.out) statically links in library code (from.alibrary files), thengccembeds copies of library functions from the.afile in the resultinga.outfile. All calls to library functions by the application are bound to the locations in thea.outfile to which the library function is copied. Binding associates a name with a location in the program memory. For example, binding a call to a library function namedgofishmeans replacing the use of the function name with the address in memory of the function (in later chapters we discuss memory addresses in more detail.)If, however, the
a.outwas created by dynamically linking a library (from library shared object,.so, files), thena.outdoes not contain a copy of the library function code from these libraries. Instead, it contains information about which dynamically linked libraries are needed by thea.outfile to run it. Such executables require an additional linking step at runtime.
如果
a.out在链接编辑(第 4 步)时与共享目标文件链接,就需要运行时链接步骤(runtime linking step)。这种情况下,动态库代码(在.so文件中)必须在运行时加载并与正在运行的程序链接。这种在运行时加载和链接共享目标库的过程称为动态链接(dynamic linking)。当用户运行一个依赖共享目标的a.out可执行文件时,系统会在程序开始执行main函数之前完成动态链接。编译器在链接编辑这一编译步骤(第 4 步)中,把共享目标依赖信息加入
a.out文件。程序开始执行时,动态链接器检查共享目标依赖列表,找到这些共享目标文件并把它们加载到正在运行的程序中。然后,它更新a.out文件中的重定位表项,把程序对共享目标中符号的使用(例如对库函数的调用)绑定到运行时加载的.so文件中这些符号的位置。如果动态链接器找不到可执行文件所需的某个共享目标(.so)文件,运行时链接就会报错。ldd工具可以列出可执行文件所依赖的共享目标:1
$ ldd a.out
GNU 调试器(GDB)可以检查正在运行的程序,显示在运行时加载和链接了哪些共享目标代码。我们会在第 3 章中介绍 GDB。不过,用于在运行时链接对动态链接库函数调用的过程查找表(Procedure Lookup Table,PLT),其检查细节超出了本教材的范围。
The runtime linking step is needed if
a.outwas linked with shared object files during link editing (step 4). In such cases, the dynamic library code (in.sofiles) must be loaded at runtime and linked with the running program. This runtime loading and linking of shared object libraries is called dynamic linking. When a user runs ana.outexecutable with shared object dependencies, the system performs dynamic linking before the program begins executing itsmainfunction.The compiler adds information about shared object dependencies into the
a.outfile during the link editing compilation step (step 4). When the program starts executing, the dynamic linker examines the list of shared object dependencies and finds and loads the shared object files into the running program. It then updates relocation table entries in thea.outfile, binding the program’s use of symbols in shared objects (such as calls to library functions) to their locations in the.sofile loaded at runtime. Runtime linking reports errors if the dynamic linker cannot find a shared object (.so) file needed by the executable.The
lddutility lists an executable file’s shared object dependencies:
1 $ ldd a.outThe GNU debugger (GDB) can examine a running program and show which shared object code is loaded and linked at runtime. We cover GDB in Chapter 3. However, the details of examining the Procedure Lookup Table (PLT), which is used for runtime linking of calls to dynamically linked library functions, is beyond the scope of this textbook.
译注
- PLT 的正式名称是「过程链接表」(Procedure Linkage Table),原文写成了 Procedure Lookup Table。ELF 文件中的
.plt节存放的就是它。man7:elf(5) - 在 Mac 上:macOS 的可执行文件格式是 Mach-O 而不是 ELF,动态库后缀是
.dylib,没有ldd命令,对应的是otool -L a.out;运行时库搜索路径的环境变量是DYLD_LIBRARY_PATH(下文的LD_LIBRARY_PATH在 macOS 上不起作用)。
关于编译各阶段以及查看不同阶段的工具的更多细节,请参见:编译阶段。
For more details about the phases of compilation and about tools for examining different phases, see: Compilation Phases.
与编译和链接库相关的常见编译错误
Common Compilation Errors Related to Compiling and Linking Libraries
程序员忘记包含库的头文件或忘记显式链接库代码时,可能会出现几种编译和链接错误。认清与每种错误对应的 gcc 编译器错误或警告,有助于调试与使用 C 库相关的错误。
Several compilation and linking errors can occur due to the programmer forgetting to include library header files or forgetting to explicitly link in library code. Identifying the
gcccompiler error or warning associated with each of these errors will help in debugging errors related to using C libraries.
来看下面这个 C 程序,它调用了 examplelib 库中的函数 libraryfunc,该库以共享目标文件 libexamplelib.so 的形式提供:
Consider the following C program that makes a call to a function
libraryfuncfrom theexampleliblibrary, which is available as a shared object file,libexamplelib.so:
1 | #include <stdio.h> |
假设头文件 examplelib.h 包含下面示例中的定义:
Assume that the header file,
examplelib.h, contains the definitions in the following example:
1 | #define MAX 10 // a constant exported by the library |
函数原型前的 extern 前缀表示函数的定义来自另一个文件——它不在 examplelib.h 文件中,而是由库实现中的某个 .c 文件提供。
The
externprefix to the function prototype means that the function’s definition comes from another file — it’s not in theexamplelib.hfile, but instead it’s provided by one of the.cfiles in the library’s implementation.
忘记包含头文件
Forgetting to include a header file
如果程序员忘了在程序中包含 examplelib.h,编译器就会针对程序中使用的、它不认识的库函数和常量给出警告和错误。例如,如果用户在没有 #include <examplelib.h> 的情况下编译程序,gcc 会产生如下输出:
If the programmer forgets to include
examplelib.hin their program, then the compiler produces warnings and errors about the program’s use of library functions and constants that it does not know about. For example, if the user compiles their program without#include <examplelib.h>,gccwill produce the following output:
1 | # '-g': add debug information, '-c': compile to .o |
第一条编译器警告(implicit declaration of function libraryfunc)告诉程序员,编译器找不到 libraryfunc 函数的函数原型。这只是一条编译器警告,因为 gcc 会猜测该函数的返回类型是整数,并继续编译程序。然而,程序员不应忽视这类警告!它们表明程序在 myprog.c 文件中使用某个函数之前没有包含它的函数原型,这往往是因为没有包含含有该函数原型的头文件。
The first compiler warning (
implicit declaration of function libraryfunc) tells the programmer that the compiler cannot find a function prototype for thelibraryfuncfunction. This is just a compiler warning becausegccwill guess that the function’s return type is an integer and will continue compiling the program. However, programmers should not ignore warnings such as these! They indicate that the program isn’t including a function prototype before its use in themyprog.cfile, which is often due to not including a header file that contains the function prototype.
现代更新(截至 2026-11)· 隐式函数声明现在是错误
原书说调用未声明的函数「只是一条警告」,这在新编译器上已不成立:C99 标准就已删除了隐式函数声明,GCC 14 起默认把它当作错误(-Werror=implicit-function-declaration),Clang 也是如此。译者在 macOS 上用 Apple Clang 编译书中的这种代码,得到的是 error: call to undeclared function 'libraryfunc'; ISO C99 and later do not support implicit function declarations。GCC 14 移植说明
第二条编译器错误(MAX undeclared (first use in this function))源于缺少常量定义。编译器无法猜测缺失常量的值,因此这一缺失的定义会导致错误。这类「undeclared」(未声明)消息通常表明,定义某个常量或全局变量的头文件缺失或没有被正确包含。
The second compiler error (
MAX undeclared (first use in this function)) follows from a missing constant definition. The compiler cannot guess at the value of the missing constant, so this missing definition fails with an error. This type of “undeclared” message often indicates that a header file defining a constant or global variable is missing or hasn’t been properly included.
忘记链接库
Forgetting to link a library
如果程序员包含了库的头文件(如前面的清单所示),却忘了在编译的链接编辑步骤(第 4 步)中显式链接该库,gcc 会用一条 undefined reference(未定义的引用)错误来指出这一点:
If the programmer includes the library header file (as shown in the previous listing), but forgets to explicitly link in the library during the link editing step (step 4) of compilation, then
gccindicates this with anundefined referenceerror:
1 | $ gcc -g myprog.c |
这个错误来自编译器的链接器组件 ld。它表明链接器找不到 myprog.c 第 9 行调用的库函数 libraryfunc 的实现。undefined reference 错误表明需要把某个库显式链接到可执行文件中。在这个例子中,在 gcc 命令行中指定 -lexamplelib 就能修复这个错误:
This error originates from
ld, the linker component of the compiler. It indicates that the linker cannot find the implementation of the library functionlibraryfuncthat gets called at line 9 inmyprog.c. Anundefined referenceerror indicates that a library needs to be explicitly linked into the executable. In this example, specifying-lexamplelibon thegcccommand line will fix the error:
1 | $ gcc -g myprog.c -lexamplelib |
gcc 找不到头文件或库文件
gcc can’t find header or library files
如果库的头文件或实现文件不在 gcc 默认搜索的目录中,编译也会出错失败。例如,如果 gcc 找不到 examplelib.h 文件,它会给出这样的错误消息:
Compilation will also fail with errors if a library’s header or implementation files are not present in the directories that
gccsearches by default. For example, ifgcccannot find theexamplelib.hfile, it will produce an error message like this:
1 | $ gcc -c myprog.c -lexamplelib |
如果链接器在编译的链接编辑步骤中找不到要链接的库的 .a 或 .so 版本,gcc 会以如下错误退出:
If the linker cannot find a
.aor.soversion of the library to link in during the link editing step of compilation,gccwill exit with an error like the following:
1 | $ gcc -c myprog.c -lexamplelib |
类似地,如果动态链接的可执行文件找不到某个共享目标文件(例如 libexamplelib.so),它在运行时会执行失败,并给出如下错误:
Similarly, if a dynamically linked executable cannot locate a shared object file (e.g.,
libexamplelib.so), it will fail to execute at runtime with an error like the following:
1 | $ ./a.out |
要解决这类错误,程序员必须给 gcc 指定额外的选项,说明在哪里能找到库的文件。还可能需要修改 LD_LIBRARY_PATH 环境变量,让运行时链接器能找到库的 .so 文件。
To resolve these types of errors, programmers must specify additional options to
gccto indicate where the library’s files can be found. They may also need to modify theLD_LIBRARY_PATHenvironment variable for the runtime linker to find a library’s.sofile.
库路径与头文件包含路径
Library and Include Paths
编译器会自动在标准目录位置中搜索头文件和库文件。例如,系统通常把标准头文件存放在 /usr/include,把库文件存放在 /usr/lib,gcc 会自动在这些目录中查找头文件和库;gcc 还会自动在当前工作目录中搜索头文件。
The compiler automatically searches in standard directory locations for header and library files. For example, systems commonly store standard header files in
/usr/include, and library files in/usr/lib, andgccautomatically looks for headers and libraries in these directories;gccalso automatically searches for header files in the current working directory.
译注
更准确地说,#include "xxx.h"(双引号形式)会先在当前源文件所在的目录中查找;#include <xxx.h>(尖括号形式)只在 -I 指定的目录和系统目录中查找,并不搜索当前目录。
如果 gcc 找不到某个头文件或库文件,用户就必须在命令行中用 -I 和 -L 显式提供路径。例如,假设名为 libexamplelib.so 的库位于 /home/me/lib,它的头文件 examplelib.h 位于 /home/me/include。由于 gcc 默认对这些路径一无所知,要成功编译使用这个库的程序,必须明确告诉它去这些位置包含文件:
If
gcccannot find a header or a library file, then the user must explicitly provide paths on the command line using-Iand-L. For example, suppose that a library namedlibexamplelib.soexists in/home/me/lib, and its header fileexamplelib.his in/home/me/include. Becausegccknows nothing of those paths by default, it must be explicitly told to include files there to successfully compile a program that uses this library:
1 | $ gcc -I/home/me/include -o myprog myprog.c -L/home/me/lib -lexamplelib |
要在启动动态链接的可执行文件时指定动态库(例如 libexamplelib.so)的位置,可以设置 LD_LIBRARY_PATH 环境变量,使其包含库所在的路径。下面是一条示例 bash 命令,可以在 shell 提示符下运行,也可以加到 .bashrc 文件中:
To specify the location of a dynamic library (e.g.,
libexamplelib.so) when launching a dynamically linked executable, set theLD_LIBRARY_PATHenvironment variable to include the path to the library. Here’s an example bash command that can be run at a shell prompt or added to a.bashrcfile:
1 | export LD_LIBRARY_PATH=/home/me/lib:$LD_LIBRARY_PATH |
当 gcc 命令行变得很长,或者一个可执行文件需要很多源文件和头文件时,用 make 和 Makefile 来简化编译会很有帮助。这里有关于 make 与 Makefile 的更多信息。
When the
gcccommand lines get long, or when an executable requires many source and header files, it helps to simplify compilation by usingmakeand aMakefile. Here’s some more information about make and Makefiles.