When including header files, is the path case sensitive?

15,513

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

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

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

Share:
15,513
Idan K
Author by

Idan K

Updated on July 20, 2022

Comments

  • Idan K
    Idan K almost 2 years

    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.

  • Thomas Matthews
    Thomas Matthews over 14 years
    For better portability, such as moving sources files, paths should not be in source code but given to the compiler. If header files move, then all the code that references them by path must be changed and regression tested!
  • Martin York
    Martin York over 14 years
    Actually its more related to the file system used rather tahn the OS. The default is as defined as above. But on Linux there is a greater veriety of file system to choose from some of which are not case sensative.
  • Daniel Vassallo
    Daniel Vassallo over 14 years
    @Martin: Yes you're right. Thank you for the accurate observation.
  • Chris Morgan
    Chris Morgan over 10 years
    So case sensitivity isn't specified by C89/99/++11? or does the standard say that search paths and filenames are case-insensitive?
  • IInspectable
    IInspectable almost 8 years
    No, sorry, it's neither the OS nor the filesystem, as explained in my answer.
  • IInspectable
    IInspectable almost 8 years
    I'm afraid, you are wrong. It is the C++ Standard that controls this, and allows any implementation to perform the search as it sees fit.
  • NicoBerrogorry
    NicoBerrogorry about 6 years
    @ThomasMatthews I've never thought of that. How would you include your headers without using their respective sub-folders then?
  • Dan
    Dan about 6 years
    @ThomasMatthews -- (only took me 9 years to circle around and respond to this) -- I agree 25,000% with this, and advocate the approach you mention exclusively in my own work... I was just addressing the question, but it was a missed opportunity to "really" answer the question with a better suggestion. Kudos!
  • IInspectable
    IInspectable over 4 years
    And even the remarks on file system case-sensitivity is wrong. Windows predominantly uses NTFS. NTFS is case-sensitive. The I/O implementation in the Windows API is case-insensitive.