[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’.

  • 要保持缺省目录、环境变量尽量纯净,主要第一种方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值