c++ - 如何在 C 和/或 C++ 中包含文件

标签 c++ c include c-preprocessor

我有一个头文件,我试图使用 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-charh- 之外char 规范。我认为你没有太多选择,只能更改文件名。

draft C++ standard 中的语法相同, 2.9 header 名称 部分。

关于c++ - 如何在 C 和/或 C++ 中包含文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18258255/

相关文章:

c - 将链接器脚本文件链接到源代码

php - 包含/需要 HTTP 地址

c++ - 为 MMO 登录服务生成安全 token 的算法

c++ - Pi 计算器程序每次运行时都会给出不同的输出

c - 如何仅使用 2 个字节的 int 的 10 位?

c++ - 在以下情况下从套接字读取数据已损坏?

javascript - 如何在 Chrome 控制台中包含 JavaScript 文件或库?

C++ #include 守卫

c++ - 如果不需要,避免在 Make 中链接和编译

c++ - 在 Qt 5.3 中播放 WAV 文件