C ++ #include查找有任何标准要求吗? [英] Is there any standard requirement for C++ #include lookup?

查看:93
本文介绍了C ++ #include查找有任何标准要求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我了解到 gnu cpp和msvc cl对待 #include 文件有所不同。常见的行为是查看includ_ing_文件旁边的目录,然后遍历包含路径(显然使用 -I / I 编译器参数。)

Recently I learned that gnu cpp and msvc cl treat #include files differently. The common behavior is to look inside the directory of next to the includ_ing_ file, and then iterate over the include path (which is obviously set using -I or /I compiler arguments.)

但是现在我想知道:我学习正确吗,或者实际上也有预处理器的标准吗?

But now I wonder: did I learn correctly, or is there actually a standard for the preprocessor, too?

推荐答案

根据标准,两个 #include< ...> #include ... 将搜索实现定义的位置集。甚至没有要求<> 之间提供的名称只是文件名

According to the standard, both #include <...> and #include "..." shall search an implementation defined set of places. There is not even a requirement that the name provided between <> or "" is a file name, just that it allows unique identification of a header.

前面已经说过,如果实现希望对编译现有源代码有任何希望,则最好遵循与其余:< ...> 指系统包含文件,而 ... 指系统您自己的包含文件(并且应首先搜索包含源文件的目录)。这些都不是标准的要求,但是没有它们,您将无法编译几乎所有现有代码。

Having said that, if an implementation wants to have any hope of compiling existing source, it had better follow the same conventions as the rest: <...> refers to a system include file, and "..." refers to your own include files (and the directory containing the source file should be searched first). None of these are requirements from the standard, but without them you have no way to compile pretty much any existing code out there.

该标准在某些地方有点精神分裂-在一个地方,它将为您提供处理文件和目录的工具(C ++ 17中的文件系统扩展名),而在另一些情况下,它拒绝承认文件和目录之类的东西甚至存在。大概是因为作者不想太乱使用古代文字,因为它现在可以正常工作,并且可能会偶然破坏某些东西,但确实看起来有些可笑。

The standard is a little schizophrenic in places - in one place it will give you tools to deal with files and directory (the filesystem extension in C++17), while in others it refuses to acknowledge that such things as files and directories even exist. Presumably this is because the authors do not want to mess with ancient text too much, since it works fine now and might just accidentally break stuff, but it does look a bit funny.

这篇关于C ++ #include查找有任何标准要求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆