[An Introduction to GCC 学习笔记] 06 链接外部库头文件
Posted 漫小牛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[An Introduction to GCC 学习笔记] 06 链接外部库头文件相关的知识,希望对你有一定的参考价值。
1 Linking with External Libraries
- To enable the compiler to link the external library, we need to supply the library in the gcc command line:
$ gcc -Wall main.c /usr/lib/libm.a -o calc
- To avoid the need to specify long paths on the command line, the compiler provides a short-cut option ‘-l’ for linking against libraries.
$ gcc -Wall main.c -lm -o calc
- In general, the compiler option ‘-1NAME’ will attempt to link object files with a library file ‘libNAME.a’ in the standard library directories.
2 Linking Order of Libraries
- The ordering of libraries on the command line follows the same convention as for object files: they are searched from left to right - a library containing the definition of a function should appear after any source files or object files which use it.
- As for object files, most current compilers will search all libraries regardless of order. However, since not all compilers do this it is best to follow the convention of ordering libraries from left to right.
- When using a library it is essential to include the appropriate header files, in order to declare the function arguments and return values with the correct types. Without declarations, the arguments of a function can be passed with the wrong type, causing corrupted results.
3 Common Problem in Compiling
- A common problem when compiling a program using library header files or library files is :
FILE.h: No such file or direcorty
/usr/bin/ld: cannot find library
- By default, gcc searches the following directories for header files and libraries:
/usr/local/include/
/usr/include/
/usr/local/lib/
/usr/lib/
3.1 Search Path
- The default search paths may also include additional system-dependent or site-specific directories, and the directories in the GCC installation itself. For example, on 64-bit platforms additional ‘lib64’ directories may also be searched by default.
- When additional libraries are installed in other directories it is necessary to extend the search paths, in order for the libraries to be found. The compiler options ‘-I’ and ‘-L’ add new directories to the beginning of the include path and library search path respectively.
- You should never place the absolute paths of header files in #include statements in your source code, as this will prevent the program from compiling on other systems. The ‘-I’ option should always be used to set the include path for header files.
3.2 Search Path Environment Variables
- The search paths for header files and libraries can also be controlled through environment variables in the shell. These may be set automatically for each session using the appropriate login file, such as ‘.bash_profile’.
- Additional directories can be added to the include path using the environment variable C_INCLUDE_PATH(for C header files) or CPLUS_INCLUDE_PATH(for C++ header files).
- Similarly, additional directories can be added to the link path using the environment variable LIBRARY_PATH.
- Following the standard Unix convention for search paths, several directories can be specified together in an environment variable as a colon separated list:
DIR1:DIR2:DIR3:...
3.3 Search Order
-
When environment variables and command-line options are used together the compile searches the directories in the following order:
- 1 Command-line options ‘-I’ and ‘-L’, from left to right.
- 2 Directories specified by environment variables, such as C_INCLUDE_PATH and LIBRARY_PATH
- 3 Default system directories.
-
Note: In day-to-day usage, directories are usually added to the search paths with the options ‘-I’ and ‘-L’.
-
要保持缺省目录、环境变量尽量纯净,主要第一种方式。
以上是关于[An Introduction to GCC 学习笔记] 06 链接外部库头文件的主要内容,如果未能解决你的问题,请参考以下文章
[An Introduction to GCC 学习笔记] 09 -Wall
[An Introduction to GCC 学习笔记] 14 优化问题3
[An Introduction to GCC 学习笔记] 14 优化问题3
[An Introduction to GCC 学习笔记] 13 优化问题2