I know that there are rules for using underscores in identifiers in C/C++. Are there any rules for using them in source code filenames?
For instance, are there any restrictions against beginning or ending a filename with an underscore? Or having an underscore as the last character before the .c
or .h
extension? Double underscores?
References are appreciated, if there are any.
If the source files are subject for preprocessor #include
directives, then the C and C++ standards specify a minimum set of requirements on the filename. The C standard says:
6.10.2 Source file inclusion
...
- The implementation shall provide unique mappings for sequences consisting of one or more nondigits or digits (6.4.2.1) followed by a period (.) and a single nondigit. The first character shall not be a digit. The implementation may ignore distinctions of alphabetical case and restrict the mapping to eight significant characters before the period.
... where nondigit contains the letters A-Z, a-z and underscore.
The exact same text (except for the paragraph numbers) can also be found in the C++ standard, 16.2 Source file inclusion.
Beyond that, what passes for a valid filename depends on the operating system, file system, compiler, linker and other parts of the compilation tool chain.
These days, I'd expect most modern systems to allow almost anything that isn't directly forbidden by the file system.
References
No. Files can be named whatever they want (given that the underlying file-system supports the name) - neither the C++, nor the C standard have any stake in that. There are rules about "_" in identifiers, yes, but that does not carry over to external things like file names.
Neither the C nor the C++ language (and please remember that they're two different languages) has any rules for source file names. Both specify the names for their standard headers, and follow certain conventions for those names, but those conventions are not imposed on other source files. (Note that standard headers are not necessarily even implemented as files.)
An operating system, or a file system, or a compiler, or some other part of the environment might impose some requirements.
More specifically, Unix-like systems typically permits any characters in file names other than '/'
(which is the directory path delimiter) and '\0'
(which is the string terminator), and compilers typically permit any valid file name (possibly paying attention the extension to determine which language to compile). Windows disallows some other characters. Case-sensitivity varies from one system to another; foo.c
and Foo.c
may or may not name the same file. The latter can be significant for foo.c
vs. foo.C
; sometimes .C
is used as an extension for C++ source. Use something else if your code might be used on a case-insensitive file system (Windows, MacOS).
There are a number of conventions for file extensions used to identifier the contents of a source file, such as .c
for C source, .h
for a header file, .cpp
or .cc
or .cxx
for C++ source, and so on. Consult your compiler's documentation.
来源:https://stackoverflow.com/questions/41684076/any-rules-about-underscores-in-filenames-in-c-c