开发者

When including header files, is the path case sensitive?

开发者 https://www.devze.com 2022-12-14 13:57 出处:网络
Given this directory tree: src/MyLibrary/MyHeader.h src/file.cpp file.cpp: #include \"mylibrary/myheader.h\"

Given this directory tree:

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

file.cpp:

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

Co开发者_如何转开发mpiling file.cpp works with VS, fails in 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.


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

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.


What does the standard say?

Case sensitivity in #include directives is controlled by the implementation (compiler/preprocessor). This is explained under 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.

Similarly, 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.

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 #include directives.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号