c++ - 如何在包含中使用预处理器宏?

标签 c++ c preprocessor freetype2

我正在尝试构建 freetype2使用我自己的构建系统(我不想使用 Jam,我准备花时间弄清楚它)。我在标题中发现了一些奇怪的东西。 Freetype 是这样定义宏的:

#define FT_CID_H  <freetype/ftcid.h>

然后像这样使用它们:

#include FT_CID_H 

我不认为这是可能的,事实上 Clang 3.9.1 提示:

error: expected "FILENAME" or <FILENAME>
#include FT_CID_H
  • 这些宏背后的基本原理是什么?
  • 这是有效的 C/C++ 吗?
  • 如何说服 Clang 解析这些 header ?

这与 How to use a macro in an #include directive? 有关但有所不同,因为这里的问题是关于编译 freetype,而不是编写新代码。

最佳答案

我将不按顺序回答你的三个问题。

Question 2

Is this valid C/C++?

是的,这确实有效。宏扩展可用于生成 #include 的最终版本指示。引用 C++14 (N4140) [cpp.include] 16.2/4:

A preprocessing directive of the form

# include pp-tokens new-line

(that does not match one of the two previous forms) is permitted. The preprocessing tokens after include in the directive are processed just as in normal text (i.e., each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens). If the directive resulting after all replacements does not match one of the two previous forms, the behavior is undefined.

提到的“以前的形式”是#include "..."#include <...> .所以是的,使用扩展到要包含的 header /文件的宏是合法的。

Question 1

What is the rationale behind these macros?

我不知道,因为我从未使用过 freetype2 库。这个问题最好由其支持 channel 或社区来回答。

Question 3

How can I convince Clang to parse these headers?

因为这是合法的 C++,所以您不必做任何事情。事实上,用户@Fanael 有 demonstrated Clang 能够解析此类代码。您的设置中一定有其他问题,或者您没有显示的其他问题。

关于c++ - 如何在包含中使用预处理器宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42858048/

相关文章:

c - C 内核级多线程

c - 缩进 : space after function name in definition

c# - 使用 Roslyn 有选择地删除预处理器指令

c++ - 对象构造期间缓冲区太小。觉得跟strcpy_s()有关系

c++ - C++ 和 Matlab 互相关

c++ - 稀疏数据的C++/GNUPlot热图?

c - 访问文件外部的静态函数

c - 重复完全相同的代码段 1000 次而不循环

Intellij IDEA 中的 Java 预处理支持

c++ - Qt打不开新窗口