我有一个头文件,我试图使用 include
从另一个源文件中包含它预处理器目录。我曾尝试同时使用引号形式和角括号形式,但似乎都无法胜任。
文件名为.>"hello.h
以及编译器搜索它的目录。我试过像这样包含它:
-
#include <.>"hello.h>
-
#include <.\>"hello.h>
-
#include <.\>\"hello.h>
-
#include ".>"hello.h"
-
#include ".>\"hello.h"
我还尝试了不同的 C 和 C++ 编译器——clang、gcc、clang++ 和 g++。
显然,以上方法均无效,否则就没有问题了。
我想这个名字可能不符合标准。不幸的是,我手头既没有C 也没有C++ 标准规范。我能找到的唯一权威信息来源是this MSDN关于 #include
的页面指令和 GNU C 预处理器文档,here . GNU的文档并没有多说,MSDN有如下条款,不过:
The path-spec is a file name optionally preceded by a directory specification. The file name must name an existing file. The syntax of the path-spec depends on the operating system on which the program is compiled.
我很好奇 C 和 C++ 标准对此有何看法?
在哪里可以找到针对 C 和 C++ 头文件命名要求的操作系统特定规则?我对 OS X、Linux 和 FreeBSD 特别感兴趣。
为什么转义 <
和/或 "
字符不起作用?
如何包含我的文件?
最佳答案
我认为您对 draft C99 standard
中的那个文件名不走运。 6.4.7 Header names
部分语法如下:
header-name:
< h-char-sequence >
" q-char-sequence "
h-char-sequence:
h-char
h-char-sequence h-char
h-char:
any member of the source character set except
the new-line character and >
q-char-sequence:
q-char
q-char-sequence q-char
q-char:
any member of the source character set except
the new-line character and "
您的文件名中同时包含 "
和 >
,这将您排除在 q-char
和 h- 之外char
规范。我认为你没有太多选择,只能更改文件名。
draft C++ standard
中的语法相同, 2.9 header 名称
部分。
关于c++ - 如何在 C 和/或 C++ 中包含文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18258255/