<2012 12 15> C标准库&POSIX标准库及其移植

在LINUX程序设计当中,经常会遇到头文件包含的问题,那么这些头文件到底在哪个路径下?具体的头文件路径依赖于程序性质(应用程序还是和内核相关的程序?本地编译还是交叉编译?)和编译器,下面分别叙述。

机器环境如下:UBUNTU10.04LTS,自己另外安装的LINUX内核源码目录为/usr/src/kernel,交叉链接器arm-linux-gcc安装目录为/usr/lcoal/arm/2.95.3/
【1.如果是应用程序,并且使用GCC进行普通编译】,如果编译时没有使用-I选项指定包含目录的话,那么默认的头文件目录在/usr/include下,可以在shell下输入如下命令
 

echo 'main(){}'|gcc ---

看到如下输出内容
 

Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure ---with-pkgversion='Ubuntu 4.4.3-4ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch--enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.--program-suffix=-4.--enable-nls --enable-clocale=gnu --enable-libstdcxx-debug--enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.4.(Ubuntu 4.4.3-4ubuntu5) 
COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=i486'
 /usr/lib/gcc/i486-linux-gnu/4.4.3/cc1 --quiet -- -D_FORTIFY_SOURCE=-mtune=generic -march=i486 -fstack-protector
ignoring nonexistent directory "/usr/local/include/i486-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/i486-linux-gnu/4.4.3/http://www.cnblogs.com/http://www.cnblogs.com/i486-linux-gnu/include"
ignoring nonexistent directory "/usr/include/i486-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.4.3/include
 /usr/lib/gcc/i486-linux-gnu/4.4.3/include-fixed
 /usr/include
End of search list.
# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "<stdin>"
main(){}
COMPILER_PATH=/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/4.4.3/:/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../:/lib/:/usr/lib/:/usr/lib/i486-linux-gnu/
COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=i486'


这就是内定的include文件搜索路径
 

#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/i486-linux-gnu/4.4.3/include
 /usr/lib/gcc/i486-linux-gnu/4.4.3/include-fixed
 /usr/include
End of search list.

实际上,这些内定的include文件搜索路径,是存放在specs文件中
 
【2.如果是应用程序,采用交叉编译】,编译时没有使用-I选项指定包含目录的话,那么默认的头文件搜索路径为/usr/local/arm/2.95.3/arm-linux/include,即交叉编译器的安装目录下的include目录,执行如下命令


echo 'main(){}'|arm-linux-gcc ---

看到如下输出内容
 

Reading specs from /usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/specs
gcc version 2.95.3 20010315 (release)
 /usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/cpp0 -lang---D__GNUC__=2-D__GNUC_MINOR__=95 -Dunix -D__arm__ -Dlinux -D__ELF__ -D__unix__ -D__arm__ -D__linux__-D__ELF__ -D__unix -D__linux -Asystem(unix) -Asystem(posix) -Acpu(arm) -Amachine(arm)-D__CHAR_UNSIGNED__ -D__ARM_ARCH_3__ -D__APCS_32__ -
GNU CPP version 2.95.3 20010315 (release) (ARM GNU/Linux with ELF)
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/include
 /usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/../../../../arm-linux/sys-include
 /usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/../../../../arm-linux/include
End of search list.
The following default directories have been omitted from the search path:
 /usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/../../../../include/g++-3
End of omitted list.
# 1 ""
main(){}

这就是内定的include文件搜索路径
 

#include "..." search starts here:
#include <...> search starts here:
 /usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/include
 /usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/../../../../arm-linux/sys-include
 /usr/local/arm/2.95.3/lib/gcc-lib/arm-linux/2.95.3/../../../../arm-linux/include
End of search list.

即在采用交叉编译时,应用程序所包含的头文件是在交叉编译器的安装目录下。

【3.如果是和内核相关的程序】,比如驱动程序,那么所包含的头文件是在内核源码的安装目录下,即/usr/src/kernel/include,这时一般要在Makefile文件中指定内核源码目录,在编译时指定INCLUDE目录,可参考“ http://blogold.chinaunix.net/u3/119151/showart_2342316.html文章中Makefile文件内容
 
【4.总结】:
nclude的header文件,连结数据库,系统定义,总共有下列来源指定编译器去那找。
当初在编译时指定的(在~gcc/gcc/collect2.c:locatelib() ,写在specs内的 
后来用-D -I -L指定的 
gcc环境变量设定(编译的时候) 
ld.so的环境变量(这是run time的时候)

========================================================================

在 C语言程序设计里,C 标准函数库(C Standard library)收集所有目前符合标准的头文件(head file),以及常用的函数库实现程序,例如 I/O 输入输出和字符串控制。1995年,Normative Addendum 1 (NA1)批准了三个头文件(iso646.h, wchar.h, and wctype.h)增加到C标准函数库中。C99标准增加了六个头文件(complex.h, fenv.h, inttypes.h, stdbool.h, stdint.h, and tgmath.h)。C11标准中又新增了5个头文件(stdalign.h, stdatomic.h, stdnoreturn.h, threads.h, and uchar.h)。至此,C标准函数库共29个头文件:

名字 源自 描述
<assert.h>   Contains the assert macro, used to assist with detecting logical errors and other types of bug in debugging versions of a program.
<complex.h> C99 A set of functions for manipulating 复数.
<ctype.h>   Defines set of functions used to classify characters by their types or to convert between upper and lower case in a way that is independent of the used character set (typically ASCII or one of its extensions, although implementations utilizing EBCDIC are also known).
<errno.h>   For testing error codes reported by library functions.
<fenv.h> C99 Defines a set of functions for controlling 浮点数 environment.
<float.h>   Defines macro constants specifying the implementation-specific properties of the 浮点数 library.
<inttypes.h> C99 Defines exact width integer types.
<iso646.h> NA1 Defines several macros that are equivalent to some of the operators in C. For programming in ISO 646 variant character sets.
<limits.h>   Defines macro constants specifying the implementation-specific properties of the integer types.
<locale.h>   Defines C localization functions.
<math.h>   Defines C mathematical functions.
<setjmp.h>   Declares the macros setjmp and longjmp, which are used for non-local exits.
<signal.h>   Defines C signal handling functions.
<stdalign.h> C11 For querying and specifying the data structure alignment of objects.
<stdarg.h>   For accessing a varying number of arguments passed to functions.
<stdatomic.h> C11 For atomic operations on data shared between threads.
<stdbool.h> C99 Defines a boolean data type.
<stddef.h>   Defines several useful types and macros.
<stdint.h> C99 Defines exact width integer types.
<stdio.h>   Defines core input and output functions
<stdlib.h>   Defines numeric conversion functions, pseudo-random numbers generation functions, dynamicmemory allocation, process control functions
<stdnoreturn.h> C11 For specifying non-returning functions.
<string.h>   Defines C string handling functions.
<tgmath.h> C99 Defines type-generic mathematical functions.
<threads.h> C11 Defines functions for managing multiple threads as well as mutexes and condition variables.
    Defines date and time handling functions
<uchar.h> C11 Types and functions for manipulating Unicode characters.
<wchar.h> NA1 Defines wide string handling functions.
<wctype.h> NA1 Defines set of functions used to classify wide characters by their types or to convert between upper and lower case

 

 ============================================================

POSIX标准定义的头文件
<dirent.h>        目录项
<fcntl.h>         文件控制
<fnmatch.h>    文件名匹配类型
<glob.h>    路径名模式匹配类型
<grp.h>        组文件
<netdb.h>    网络数据库操作
<pwd.h>        口令文件
<regex.h>    正则表达式
<tar.h>        TAR归档值
<termios.h>    终端I/O
<unistd.h>    符号常量
<utime.h>    文件时间
<wordexp.h>    字符扩展类型
-------------------------
<arpa/inet.h>    INTERNET定义
<net/if.h>    套接字本地接口
<netinet/in.h>    INTERNET地址族
<netinet/tcp.h>    传输控制协议定义
-------------------------  
<sys/mman.h>    内存管理声明
<sys/select.h>    Select函数
<sys/socket.h>    套接字借口
<sys/stat.h>    文件状态
<sys/times.h>    进程时间
<sys/types.h>    基本系统数据类型
<sys/un.h>    UNIX域套接字定义
<sys/utsname.h>    系统名
<sys/wait.h>    进程控制
 
------------------------------
POSIX定义的XSI扩展头文件
<cpio.h>    cpio归档值  
<dlfcn.h>    动态链接
<fmtmsg.h>    消息显示结构
<ftw.h>        文件树漫游
<iconv.h>    代码集转换使用程序
<langinfo.h>    语言信息常量
<libgen.h>    模式匹配函数定义
<monetary.h>    货币类型
<ndbm.h>    数据库操作
<nl_types.h>    消息类别
<poll.h>    轮询函数
<search.h>    搜索表
<strings.h>    字符串操作
<syslog.h>    系统出错日志记录
<ucontext.h>    用户上下文
<ulimit.h>    用户限制
<utmpx.h>    用户帐户数据库  
-----------------------------
<sys/ipc.h>    IPC(命名管道)
<sys/msg.h>    消息队列
<sys/resource.h>资源操作
<sys/sem.h>    信号量
<sys/shm.h>    共享存储
<sys/statvfs.h>    文件系统信息
<sys/time.h>    时间类型
<sys/timeb.h>    附加的日期和时间定义
<sys/uio.h>    矢量I/O操作
 
------------------------------
POSIX定义的可选头文件
<aio.h>        异步I/O
<mqueue.h>    消息队列
<pthread.h>    线程
<sched.h>    执行调度
<semaphore.h>    信号量
<spawn.h>     实时spawn接口
<stropts.h>    XSI STREAMS接口
<trace.h>     事件跟踪

C99增加的部分
#include <complex.h>    复数处理
#include <fenv.h>      浮点环境
#include <inttypes.h>    整数格式转换
#include <stdbool.h>    布尔环境
#include <stdint.h>     整型环境
#include <tgmath.h>    通用类型数学宏

你可能感兴趣的:(2012)