Any rules about underscores in filenames in C/C++? [closed]

北战南征 提交于 2019-12-07 07:28:46

问题


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.


回答1:


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

...

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

  • The final public draft of the C11 standard, n1570
  • The final public draft of the C++11 standard, n3337



回答2:


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.




回答3:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!