在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 -E -v -
看到如下输出内容
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --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.4 --program-suffix=-4.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.3 (Ubuntu 4.4.3-4ubuntu5)
COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=i486'
/usr/lib/gcc/i486-linux-gnu/4.4.3/cc1 -E -quiet -v - -D_FORTIFY_SOURCE=2 -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/../../../../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 ""
# 1 ""
# 1 ""
# 1 ""
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 -E -v -
看到如下输出内容
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-c -v -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目录,可参考“
【4.总结】:
nclude的header文件,连结数据库,系统定义,总共有下列来源指定编译器去那找。
当初在编译时指定的(在~gcc/gcc/collect2.c:locatelib() ,写在specs内的
后来用-D -I -L指定的
gcc环境变量设定(编译的时候)
ld.so的环境变量(这是run time的时候)