This is quite a contrived problem, I admit, but here it is.
Suppose you have a file with the >
character in its name. This is possible on most Unix systems afaik:
$ touch 'weird>name'
$ ls -l
-rw-r--r-- 1 user user 0 28 Mag 11:05 weird>name
Now, suppose this file contains C/C++ code and you want to include it as an header:
#include <weird>name>
int main() {
return weird_function();
}
Clang gives me the following error:
test.cpp:1:10: fatal error: 'weird' file not found
#include <weird>name>
Of course, since the preprocessor parses the directive up to the first >
and looks for the weird
file. But, I wonder if some escaping mechanism exists to allow me to include the right file.
So, in C and/or C++, is there a way to include an header file which has the >
character in its name?
Edit: Many suggested me why not to use #include "weird>name"
. I admit that my mind slipped over the quotes syntax while writing the question, but it remains valid because the two syntaxes may ask the compiler to search in different paths (theoretically at least). So is there any escaping mechanism to let me include weird>name
using the #include <>
syntax?
So, in C and/or C++, is there a way to include an header file which has the > character in its name?
Yes:
#include "weird>name"
So is there any escaping mechanism to let me include
weird>name
using the#include <>
syntax?
No. The characters between the <
and >
must be "any member of the source character set except new-line and >
" ([lex.header]). Any escaped form of Edit: Implementations are allowed to support implementation-defined escape sequences there though (see [lex.header] p2 and its footnote).>
would still be a way to represent the >
character, which is not allowed.
The #include " q-char-sequence "
form does allow the >
character to appear, even though it might get reprocessed as #include <...>
if searching as "..."
fails ([cpp.include] p3).
The preprocessor also allows another form ([cpp.include] p4](http://eel.is/c++draft/cpp.include#4)), but its effect are implementation-defined, and the implementations I tried do not allow joining weird
and >
and name
into a single preprocessor-token that can then be included
Ask the author of your compiler.
The C and C++ standards grant a lot of leeway to implementations over the interpretation of #include
directives. There's no requirement that #include <foo.h>
causes the inclusion of a file called "foo.h". For instance, a compiler can choose to ROT13 all the source file names if it likes. And for non-alphanumeric characters, the implementation can identify and remap certain character sequences. So if there were a platform where >
regularly showed up in filenames, it's likely that a compiler for that platform would specify that, say, \g
or something would be remapped to >
. But the standard doesn't mandate a particular encoding.
Incidentally, the implementation could also just choose to allow #include <weird>name>
. Since that is not well-formed under the language standards, an implementation is free to define a meaning for it as an extension.
Try below syntax:
#include "weird>name"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With