查看 GCC编译时头文件搜索路径

5200阅读 0评论2015-03-31 abin9630
分类:LINUX

对于使用 gcc编译时候,有时候回去查看相关的头文件的声明

但是对于有些头文件存放位置,却不在通常的 /usr/include目录下,比如 stdarg.h

可以通过以下方式来查看 GCC编译时候所使用的默认搜索路径
首先,使用 gcc --help来查看 gcc支持的命令

点击(此处)折叠或打开

  1. [martin@linux-2.6.11]$ gcc --help
  2. Usage: gcc [options] file...
  3. Options:
  4.   -pass-exit-codes Exit with highest error code from a phase
  5.   --help Display this information
  6.   --target-help Display target specific command line options
  7.   --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]
  8.                            Display specific types of command line options
  9.   (Use '-v --help' to display command line options of sub-processes)
  10.   --version Display compiler version information
  11.   -dumpspecs Display all of the built in spec strings
  12.   -dumpversion Display the version of the compiler
  13.   -dumpmachine Display the compiler's target processor
  14.   -print-search-dirs Display the directories in the compiler's search path
  15.   -print-libgcc-file-name Display the name of the compiler's companion library
  16.   -print-file-name= Display the full path to library
  17.   -print-prog-name= Display the full path to compiler component
  18.   -print-multiarch Display the target's normalized GNU triplet, used as
  19.                            a component in the library path
  20.   -print-multi-directory Display the root directory for versions of libgcc
  21.   -print-multi-lib Display the mapping between command line options and
  22.                            multiple library search directories
  23.   -print-multi-os-directory Display the relative path to OS libraries
  24.   -print-sysroot Display the target libraries directory
  25.   -print-sysroot-headers-suffix Display the sysroot suffix used to find headers
  26.   -Wa,<options> Pass comma-separated <options> on to the assembler
  27.   -Wp,<options> Pass comma-separated <options> on to the preprocessor
  28.   -Wl,<options> Pass comma-separated <options> on to the linker
  29.   -Xassembler <arg> Pass <arg> on to the assembler
  30.   -Xpreprocessor <arg> Pass <arg> on to the preprocessor
  31.   -Xlinker <arg> Pass <arg> on to the linker
  32.   -save-temps Do not delete intermediate files
  33.   -save-temps=<arg> Do not delete intermediate files
  34.   -no-canonical-prefixes Do not canonicalize paths when building relative
  35.                            prefixes to other gcc components
  36.   -pipe Use pipes rather than intermediate files
  37.   -time Time the execution of each subprocess
  38.   -specs=<file> Override built-in specs with the contents of <file>
  39.   -std=<standard> Assume that the input sources are for <standard>
  40.   --sysroot=<directory> Use <directory> as the root directory for headers
  41.                            and libraries
  42.   -B <directory> Add <directory> to the compiler's search paths
  43.   -v Display the programs invoked by the compiler
  44.   -### Like -v but options quoted and commands not executed
  45.   -E Preprocess only; do not compile, assemble or link
  46.   -S Compile only; do not assemble or link
  47.   -c Compile and assemble, but do not link
  48.   -o Place the output into
  49.   -pie Create a position independent executable
  50.   -shared Create a shared library
  51.   -x Specify the language of the following input files
  52.                            Permissible languages include: c c++ assembler none
  53.                            'none' means revert to the default behavior of
  54.                            guessing the language based on the file's extension

  55. Options starting with -g, -f, -m, -O, -W, or --param are automatically
  56.  passed on to the various sub-processes invoked by gcc. In order to pass
  57.  other options on to these processes the -W<letter> options must be used.

  58. For bug reporting instructions, please see:
  59. <file:///usr/share/doc/gcc-4.8/README.Bugs>.
  60. [martin@linux-2.6.11]$

其中有个 print-search-dirs,会罗列以下的搜罗目录,包括lib,.h文件等等(对比后发现是有重复的)

点击(此处)折叠或打开

  1. [martin@linux-2.6.11]$ gcc -print-search-dirs
  2. install: /usr/lib/gcc/i686-linux-gnu/4.8/
  3. programs: =/usr/lib/gcc/i686-linux-gnu/4.8/:/usr/lib/gcc/i686-linux-gnu/4.8/:/usr/lib/gcc/i686-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.8/:/usr/lib/gcc/i686-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.8/../../../../i686-linux-gnu/bin/i686-linux-gnu/4.8/:/usr/lib/gcc/i686-linux-gnu/4.8/../../../../i686-linux-gnu/bin/i386-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.8/../../../../i686-linux-gnu/bin/
  4. libraries: =/usr/lib/gcc/i686-linux-gnu/4.8/:/usr/lib/gcc/i686-linux-gnu/4.8/../../../../i686-linux-gnu/lib/i686-linux-gnu/4.8/:/usr/lib/gcc/i686-linux-gnu/4.8/../../../../i686-linux-gnu/lib/i386-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.8/../../../../i686-linux-gnu/lib/../lib/:/usr/lib/gcc/i686-linux-gnu/4.8/../../../i686-linux-gnu/4.8/:/usr/lib/gcc/i686-linux-gnu/4.8/../../../i386-linux-gnu/:/usr/lib/gcc/i686-linux-gnu/4.8/../../../../lib/:/lib/i686-linux-gnu/4.8/:/lib/i386-linux-gnu/:/lib/../lib/:/usr/lib/i686-linux-gnu/4.8/:/usr/lib/i386-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/i686-linux-gnu/4.8/../../../../i686-linux-gnu/lib/:/usr/lib/gcc/i686-linux-gnu/4.8/../../../:/lib/:/usr/lib/
  5. [martin@linux-2.6.11]$

头文件的搜索,可以直接使用 -print-prog-name=来实现
    1. 对于 c的头文件,使用-print-prog-name=cc1
    2. 对于c++的头文件, 使用 -print-prog-name=cc1plus
如果直接使用 gcc -print-prog-name=cc1,会打印 cc1的程序路径,比如

点击(此处)折叠或打开

  1. [martin@linux-2.6.11]$ gcc -print-prog-name=cc1
  2. /usr/lib/gcc/i686-linux-gnu/4.8/cc1
  3. [martin@linux-2.6.11]$
  4. [martin@linux-2.6.11]$ gcc -print-prog-name=cc1plus
  5. /usr/lib/gcc/i686-linux-gnu/4.8/cc1plus
  6. [martin@linux-2.6.11]$
但是不会显示具体的搜索路径。如果需要显示路径,则需要执行该cc1程序,附带使用 -v选项(cc1有很多其他的选项,没有研究使用方法)
执行的方式有两种:
    1. 直接执行该文件
    2. 借助shell中的 `来实现 gcc -print-prog-name=cc1的程序

点击(此处)折叠或打开

  1. [martin@linux-2.6.11]$ `gcc -print-prog-name=cc1` -v
  2. ignoring nonexistent directory "/usr/lib/gcc/i686-linux-gnu/4.8/../../../../i686-linux-gnu/include"
  3. #include "..." search starts here:
  4. #include <...> search starts here:
  5.  /usr/lib/gcc/i686-linux-gnu/4.8/include
  6.  /usr/local/include
  7.  /usr/lib/gcc/i686-linux-gnu/4.8/include-fixed
  8.  /usr/include
  9. End of search list.
  10. ^C^C
  11. [martin@linux-2.6.11]$
  12. [martin@linux-2.6.11]$ /usr/lib/gcc/i686-linux-gnu/4.8/cc1 -v
  13. ignoring nonexistent directory "/usr/lib/gcc/i686-linux-gnu/4.8/../../../../i686-linux-gnu/include"
  14. #include "..." search starts here:
  15. #include <...> search starts here:
  16.  /usr/lib/gcc/i686-linux-gnu/4.8/include
  17.  /usr/local/include
  18.  /usr/lib/gcc/i686-linux-gnu/4.8/include-fixed
  19.  /usr/include
  20. End of search list.
  21. ^C
  22. [martin@linux-2.6.11]$
  23. [martin@linux-2.6.11]$
  24. [martin@linux-2.6.11]$ `gcc -print-prog-name=cc1plus` -v
  25. ignoring nonexistent directory "/usr/lib/gcc/i686-linux-gnu/4.8/../../../../i686-linux-gnu/include"
  26. #include "..." search starts here:
  27. #include <...> search starts here:
  28.  /usr/include/c++/4.8
  29.  /usr/include/i386-linux-gnu/c++/4.8
  30.  /usr/include/c++/4.8/backward
  31.  /usr/lib/gcc/i686-linux-gnu/4.8/include
  32.  /usr/local/include
  33.  /usr/lib/gcc/i686-linux-gnu/4.8/include-fixed
  34.  /usr/include
  35. End of search list.
  36. ^C
  37. [martin@linux-2.6.11]$
  38. [martin@linux-2.6.11]$ /usr/lib/gcc/i686-linux-gnu/4.8/cc1plus -v
  39. ignoring nonexistent directory "/usr/lib/gcc/i686-linux-gnu/4.8/../../../../i686-linux-gnu/include"
  40. #include "..." search starts here:
  41. #include <...> search starts here:
  42.  /usr/include/c++/4.8
  43.  /usr/include/i386-linux-gnu/c++/4.8
  44.  /usr/include/c++/4.8/backward
  45.  /usr/lib/gcc/i686-linux-gnu/4.8/include
  46.  /usr/local/include
  47.  /usr/lib/gcc/i686-linux-gnu/4.8/include-fixed
  48.  /usr/include
  49. End of search list.
  50. ^C
  51. [martin@linux-2.6.11]$


上一篇:#, ##, args...以及__VAR_ARGS__宏定义解析
下一篇:__attribute__机制介绍