Sun Studio 12:C 用户指南

2.14.1 使用 -I- 选项更改搜索算法

新的 -I- 选项提供对缺省搜索规则的更多控制。只有命令行上的第一个 -I- 选项的作用如本节所述。当命令行上出现 -I- 时:

对于 #include "foo.h" 形式的 include 文件,按以下顺序搜索目录:

1. 使用 -I 选项指定的目录(在 -I- 前后)。

2. 编译器提供的 C++ 头文件、ANSI C 头文件和专用文件的目录。

3. /usr/include 目录。

对于 #include <foo.h> 形式的 include 文件,按以下顺序搜索目录:

1. 使用 -I 选项指定的目录(在 -I- 后面)。

2. 编译器提供的 C++ 头文件、ANSI C 头文件和专用文件的目录。

3. /usr/include 目录。

下例显示在编译 prog.c 时使用 -I- 的结果。


prog.c
#include "a.h"

#include <b.h>

#include "c.h"


c.h
#ifndef _C_H_1

#define _C_H_1

int c1;

#endif


int/a.h
#ifndef _A_H

#define _A_H

#include "c.h"

int a;

#endif


int/b.h
#ifndef _B_H

#define _B_H

#include <c.h>

int b;

#endif
int/c.h
#ifndef _C_H_2

#define _C_H_2

int c2;

#endif

以下命令显示了在当前目录(包含文件的目录)中搜索 #include "foo.h" 形式的包含语句的缺省行为。当处理 inc/a.h 中的 #include "c.h" 语句时,预处理程序包含 inc 子目录中的 c.h 头文件。当处理 prog.c 中的 #include "c.h" 语句时,预处理程序包含具有 prog.c 的目录中的 c.h 文件。请注意,-H 选项指示编译器打印所包含文件的路径。


example% cc -c -Iinc -H prog.c
inc/a.h
            inc/c.h
inc/b.h
            inc/c.h
c.h

以下命令显示了 -I- 选项的效果。当预处理程序处理 #include "foo.h" 形式的语句时,它并不首先在包含目录中查找,而是按照通过 -I 选项指定的目录在命令行上的显示顺序搜索这些目录。当处理 inc/a.h 中的 #include "c.h" 语句时,预处理程序包含 ./c.h 头文件而不是 inc/c.h 头文件。


example% cc -c -I. -I- -Iinc -H prog.c
inc/a.h
            ./c.h
inc/b.h
            inc/c.h
./c.h

2.14.1.1 警告

任何时候都不要将编译器安装区域 /usr/include/lib/usr/lib 指定为搜索目录。

有关更多信息,请参见B.2.34 -I[-| dir]