将gcc
与-I
选项一起使用时,要搜索的目录的优先级是多少?如果您的标题名称与系统标题的名称冲突,但它们位于不同的目录中会怎样?哪一个是#include
- ed?此外,系统标题包含在哪个顺序?
答案 0 :(得分:2)
这是一个中等复杂的问题,取决于您是否撰写了#include <header.h>
或#include "header.h"
以及您所包含的文件是直接包含在您的来源中还是包含在其他标题中。
通常,双引号中的名称会在保存标题源文件的目录中查找,然后在搜索尖括号中的名称的相同位置(因此#include "header.h"
查找的位置比{{#include <header.h>
更多。 1}}确实)。
您可以添加使用-I /some/other/include
命令行选项搜索的更多地点。这些按顺序在系统(默认)位置之前搜索。搜索的系统位置可能因平台而异。在Unix上,它通常包含/usr/local/include
并始终以/usr/include
结尾;它也可能包括其他地方。
如果#include
行在另一个标题中,GCC会在找到第一个标题的目录中开始搜索该嵌套标题,而不是在开头重新开始。
对于某些平台上的某些系统标头,GCC会创建标头的辅助固定副本,代替/usr/include
或其下级区域中的标头。
您可以使用命令行选项覆盖大多数这些行为。有关完整的血腥细节,请阅读GCC [预处理器]手册,了解它如何处理Header Files。 GCC编译器手册概述了在手册的Preprocessor Options部分中控制它的选项。预处理器手册在Invocation中列出了深奥的选项。
证明'当前目录'是'包含源文件的目录'。
$ mkdir x3
$ echo 'Code from x3/header1.h' > x3/header1.h
$ echo '#include "header1.h"' > x3/source.c
$ echo 'This is from the current directory, not from the subdirectory.' > header1.h
$ cpp x3/source.c
# 1 "x3/source.c"
# 1 "<command-line>"
# 1 "x3/source.c"
# 1 "x3/header1.h" 1
Code from x3/header1.h
# 1 "x3/source.c" 2
$ cpp -I . x3/source.c
# 1 "x3/source.c"
# 1 "<command-line>"
# 1 "x3/source.c"
# 1 "x3/header1.h" 1
Code from x3/header1.h
# 1 "x3/source.c" 2
$ rm x3/header1.h
$ cpp x3/source.c
# 1 "x3/source.c"
# 1 "<command-line>"
# 1 "x3/source.c"
x3/source.c:1:21: fatal error: header1.h: No such file or directory
#include "header1.h"
^
compilation terminated.
$ cpp -I . x3/source.c
# 1 "x3/source.c"
# 1 "<command-line>"
# 1 "x3/source.c"
# 1 "./header1.h" 1
This is from the current directory, not from the subdirectory.
# 1 "x3/source.c" 2
$ rm -fr header1.h x3
$
在Mac OS X 10.9.2上使用GCC 4.8.2中的cpp
进行测试。
答案 1 :(得分:0)
顺序。文件出现的第一个。
就像PATH
变量