Today I read this question Any rules about underscores in filenames in C/C++?, and I found it very interesting that the standard seems to not allow what is usually seen in many libraries (I also do it in my personal library this way):
For example, in opencv
we can see this:
// File: opencv/include/opencv2/opencv.hpp
#include "opencv2/opencv_modules.hpp"
But the standard says:
§ 6.10.2 Source file inclusion
Semantics
5 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.
nondigit
means letters (A
-Z
a
-z
) and underscore _
.
It says absolutely nothing about /
which would imply that it is forbidden to use a path, not to mention dots or hyphens in file names.
To test this first, I wrote a simple program with a source file test.c
and a header file _1.2-3~a.hh
in the same directory tst/
:
// File: test.c
#include "./..//tst//./_1.2-3~a.hh"
int main(void)
{
char a [10] = "abcdefghi";
char b [5] = "qwert";
strncpy(b, a, 5 - 1);
printf("b: \"%c%c%c%c%c\"\n", b[0], b[1], b[2], b[3], b[4]);
/* printed: b: "abcdt" */
b[5 - 1] = '\0';
printf("b: \"%c%c%c%c%c\"\n", b[0], b[1], b[2], b[3], b[4]);
/* printed: b: "abcd" */
return 0;
}
// File: _1.2-3~a.hh
#include <stdio.h>
#include <string.h>
Which I compiled with this options: $ gcc -std=c11 -pedantic-errors test.c -o tst
with no complain from the compiler (I have gcc (Debian 8.2.0-8) 8.2.0
).
- Is it really forbidden to use a relative path in an include?
/
, but the result of doing so is implementation-defined. Almost everything about the sequences between<
and>
or between"
and"
in a#include
directive is implementation-defined. The standard says that if you use a name such asabcdefgh.h
, the implementation is required to handle it as a unique (subject to case-insensitivity and not using a digit in place ofa
). The implementation may be case-sensitive; it may allow other characters — these details are implementation-defined, which means that the implementation must document its rules.