When including header files, is the path case sens

2019-01-09 15:40发布

问题:

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.

  • 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.

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.