windows 包含头文件时,路径是否区分大小写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1951951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:37:45  来源:igfitidea点击:

When including header files, is the path case sensitive?

c++windowsvisual-studiolinuxgcc

提问by Idan K

Given this directory tree:

鉴于此目录树:

src/MyLibrary/MyHeader.h
src/file.cpp

file.cpp:

#include "mylibrary/myheader.h"
...

Compiling file.cpp works with VS, fails in gcc.

编译 file.cpp 与 VS 一起工作,在 gcc 中失败。

  • What does the standard say?
  • If the path is case sensitive, why is this wise?
  • What's the best practice, keep all file/folder names lowercase and thus do the same when including?
  • 标准怎么说?
  • 如果路径区分大小写,为什么这是明智的?
  • 最佳做法是什么,将所有文件/文件夹名称保持小写,从而在包含时执行相同的操作?

Thanks.

谢谢。

回答by Daniel Vassallo

The case sensitivity depends on the Operating System. Windows is not case sensitive. Linux is.

区分大小写取决于操作系统。Windows 不区分大小写。Linux是。

EDIT:

编辑:

Actually, as observed by Martin York's comment, the case sensitivity depends on the file system. By default Windows uses a case insensitive file system, while Linux uses a case sensitive one. For whoever is interested to know which file systems are case sensitive and which aren't, there is a comprehensive list on Wikipedia: Comparison of file name limitations.

实际上,正如Martin York的评论所观察到的,区分大小写取决于文件系统。默认情况下,Windows 使用不区分大小写的文件系统,而 Linux 使用区分大小写的文件系统。对于有兴趣了解哪些文件系统区分大小写哪些不区分大小写的人,维基百科上有一个完整的列表:文件名限制比较

回答by IInspectable

What does the standard say?

标准怎么说?

Case sensitivity in #includedirectives is controlled by the implementation (compiler/preprocessor). This is explained under 16.2.2 [cpp.include]:

#include指令中的区分大小写由实现(编译器/预处理器)控制。这在 16.2.2 [cpp.include] 下解释:

A preprocessing directive of the form
# include < h-char-sequence> new-line
searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the <and >delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

该形式的预处理指令在
# include < h-char-sequence> new-line
实现定义的位置序列中搜索由<>分隔符之间的指定序列唯一标识的头,并导致该指令被头的全部内容替换。如何指定位置或标识的标头是实现定义的。

Similarly, 16.2.3 [cpp.include]:

同样,16.2.3 [cpp.include]:

A preprocessing directive of the form
# include " q-char-sequence" new-line
causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the "delimiters. The named source file is searched for in an implementation-defined manner.If this search is not supported, or if the search fails, the directive is reprocessed as if it read
# include < h-char-sequence> new-line
with the identical contained sequence (including >characters, if any) from the original directive.

该形式的预处理指令
# include " q-char-sequence" new-line
导致该指令替换为由"分隔符之间的指定序列标识的源文件的全部内容。以实现定义的方式搜索命名的源文件。如果此搜索不受支持,或者搜索失败,则重新处理该指令,就像它从原始指令中读取
# include < h-char-sequence> new-line
包含的相同序列(包括>字符,如果有)一样。

A natural choice for an implementation of the language is to use the case sensitivity of the filesystem or OS, but there is no strict requirement to do so (as all other answers suggest).

语言实现的一个自然选择是使用文件系统或操作系统的区分大小写,但没有严格要求这样做(正如所有其他答案所建议的那样)。

What's the best practice, keep all file/folder names lowercase and thus do the same when including?

最佳做法是什么,将所有文件/文件夹名称保持小写,从而在包含时执行相同的操作?

Best practice, as always: Keep things consistent. If you are using mixed-case source/header files in your project, keep using them and replicate the exact casing in your #includedirectives.

最佳实践,一如既往:保持一致。如果您在项目中使用混合大小写的源/头文件,请继续使用它们并在您的#include指令中复制准确的大小写。

回答by Dan

Another point to remember is the path separator character. Even though Visual Studio (and other Windows IDEs I'm sure) will accept either '/' or '\', you should always use '/' within an include path for portability.

要记住的另一点是路径分隔符。尽管 Visual Studio(以及我确信的其他 Windows IDE)将接受“/”或“\”,但为了可移植性,您应该始终在包含路径中使用“/”。

回答by Priyank Bolia

Its not C++ standard, its the Linux way, where all path names are case sensitive. The best practice is too chose whatever file name you want ( mostly lowercase) and use the same case in the include directive. Also always use relative file paths.

它不是 C++ 标准,而是 Linux 方式,其中所有路径名都区分大小写。最佳做法是选择您想要的任何文件名(主要是小写)并在 include 指令中使用相同的大小写。也总是使用相对文件路径。