GCC -I 指定的目录搜索先于标准库目录

GCC -I 选项的一点小笔记

-v选项可以列出寻找头文件时的搜索目录
g++ -v -I ./ test.cpp

#include "..." search starts here:
#include <...> search starts here:
 ./
 /usr/include/c++/4.1.2
 /usr/include/c++/4.1.2/i586-suse-linux
 /usr/include/c++/4.1.2/backward
 /usr/local/include
 /usr/lib/gcc/i586-suse-linux/4.1.2/include
 /usr/lib/gcc/i586-suse-linux/4.1.2/../../../../i586-suse-linux/include
 /usr/include


-I 指定的目录,在使用尖括号引用时,是先于标准头文件目录被搜索的。例如使用了 -I./,且当前目录下有一个 stdio.h,那么在 #include <stdio.h>时,所引用的是当前目录下的stdio.h,而不是系统自带的标准头文件。

可以使用 iquote 选项代替 -I,这样的话,指定的头文件目录不会用于对通过<>引用的头文件搜索。
g++ -v -iquote ./ test.cpp

#include "..." search starts here:
 ./
#include <...> search starts here:
 /usr/include/c++/4.1.2
 /usr/include/c++/4.1.2/i586-suse-linux
 /usr/include/c++/4.1.2/backward
 /usr/local/include
 /usr/lib/gcc/i586-suse-linux/4.1.2/include
 /usr/lib/gcc/i586-suse-linux/4.1.2/../../../../i586-suse-linux/include
 /usr/include


附(gcc下,<> "" 引入头文件的区别):
1. #include <xx.h> 会先搜索 -I 选项指定的目录,再搜索标准头文件目录
2. #include "xx.h" 会先搜索当前目录,再搜索-iquote 选项指定的目录, 接着搜索 <>引入方式时的目录

-nostdinc 选项:不搜索标准的头文件

-iquote 仅仅作为 ""而非<>的搜索路径


====

http://www.diybl.com/course/3_program/c++/cppjs/20101207/548284.html

====

3.14 Options for Directory Search

These options specify directories to search for header files, forlibraries and for parts of the compiler:

-I dir
Add the directory dir to the head of the list of directories to besearched for header files. This can be used to override a system headerfile, substituting your own version, since these directories aresearched before the system header file directories. However, you shouldnot use this option to add directories that contain vendor-suppliedsystem header files (use -isystem for that). If you use more thanone -I option, the directories are scanned in left-to-rightorder; the standard system directories come after.

If a standard system include directory, or a directory specified with-isystem, is also specified with -I, the -Ioption will be ignored. The directory will still be searched but as asystem directory at its normal position in the system include chain. This is to ensure that GCC's procedure to fix buggy system headers andthe ordering for the include_next directive are not inadvertently changed. If you really need to change the search order for system directories,use the -nostdinc and/or -isystem options.

-iplugindir= dir
Set the directory to search for plugins that are passedby -fplugin=name instead of -fplugin=path/name.so. This option is not meantto be used by the user, but only passed by the driver.
-iquote dir
Add the directory dir to the head of the list of directories tobe searched for header files only for the case of ` #include"file"'; they are not searched for ` #include <file>',otherwise just like -I.
-L dir
Add directory dir to the list of directories to be searchedfor -l.
-B prefix
This option specifies where to find the executables, libraries,include files, and data files of the compiler itself.

The compiler driver program runs one or more of the subprogramscpp, cc1, as and ld. It triesprefix as a prefix for each program it tries to run, both with andwithout `machine/version/' (see Target Options).

For each subprogram to be run, the compiler driver first tries the-B prefix, if any. If that name is not found, or if -Bwas not specified, the driver tries two standard prefixes,/usr/lib/gcc/ and /usr/local/lib/gcc/. If neither ofthose results in a file name that is found, the unmodified programname is searched for using the directories specified in yourPATH environment variable.

The compiler will check to see if the path provided by the -Brefers to a directory, and if necessary it will add a directoryseparator character at the end of the path.

-B prefixes that effectively specify directory names also applyto libraries in the linker, because the compiler translates theseoptions into -L options for the linker. They also apply toincludes files in the preprocessor, because the compiler translates theseoptions into -isystem options for the preprocessor. In this case,the compiler appends `include' to the prefix.

The runtime support file libgcc.a can also be searched for usingthe -B prefix, if needed. If it is not found there, the twostandard prefixes above are tried, and that is all. The file is leftout of the link if it is not found by those means.

Another way to specify a prefix much like the -B prefix is to usethe environment variable GCC_EXEC_PREFIX. See Environment Variables.

As a special kludge, if the path provided by -B is[dir/]stageN/, where N is a number in the range 0 to9, then it will be replaced by [dir/]include. This is to helpwith boot-strapping the compiler.

-specs= file
Process file after the compiler reads in the standard specsfile, in order to override the defaults which the gcc driverprogram uses when determining what switches to pass to cc1, cc1plus, as, ld, etc. More than one -specs=file can be specified on the command line, and theyare processed in order, from left to right.
--sysroot= dir
Use dir as the logical root directory for headers and libraries. For example, if the compiler would normally search for headers in /usr/include and libraries in /usr/lib, it will insteadsearch dir/usr/include and dir/usr/lib.

If you use both this option and the -isysroot option, thenthe --sysroot option will apply to libraries, but the-isysroot option will apply to header files.

The GNU linker (beginning with version 2.16) has the necessary supportfor this option. If your linker does not support this option, theheader file aspect of --sysroot will still work, but thelibrary aspect will not.

-I-
This option has been deprecated. Please use -iquote instead for -I directories before the -I- and remove the -I-. Any directories you specify with -I options before the -I-option are searched only for the case of ` #include "file"';they are not searched for ` #include <file>'.

If additional directories are specified with -I options afterthe -I-, these directories are searched for all `#include'directives. (Ordinarily all -I directories are usedthis way.)

In addition, the -I- option inhibits the use of the currentdirectory (where the current input file came from) as the first searchdirectory for `#include "file"'. There is no way tooverride this effect of -I-. With -I. you can specifysearching the directory that was current when the compiler wasinvoked. That is not exactly the same as what the preprocessor doesby default, but it is often satisfactory.

-I- does not inhibit the use of the standard system directoriesfor header files. Thus, -I- and -nostdinc areindependent.


====

http://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值