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.
Case sensitivity in
#include
directives is controlled by the implementation (compiler/preprocessor). This is explained under 16.2.2 [cpp.include]:Similarly, 16.2.3 [cpp.include]:
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).
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.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.
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.
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.