gpt4 book ai didi

c++ - #ifdef 宏

转载 作者:行者123 更新时间:2023-11-30 00:37:29 25 4
gpt4 key购买 nike

我看见了

#ifdef <token>
code;
#endif

要包含在内,但我找不到它在它包含的任何 header 中定义。是否有任何其他机制可以定义 token ?

最佳答案

首先,有些宏是由编译器隐式定义的(例如,__cplusplus)。其中一些是标准的,一些是特定于编译器的扩展。查看您的 compiler manual查看完整列表。

此外,大多数编译器允许在命令行上定义宏。确切的机制取决于编译器,但通常采用 -D/D 命令行选项的形式。例如,请参阅 gcc manual :

-D name

Predefine name as a macro, with definition 1.

-D name=definition

The contents of definition are tokenized and processed as if they appeared during translation phase three in a `#define' directive. In particular, the definition will be truncated by embedded newline characters. If you are invoking the preprocessor from a shell or shell-like program you may need to use the shell's quoting syntax to protect characters such as spaces that have a meaning in the shell syntax.

If you wish to define a function-like macro on the command line, write its argument list with surrounding parentheses before the equals sign (if any). Parentheses are meaningful to most shells, so you will need to quote the option. With sh and csh, -D'name(args...)=definition' works.

对于 Microsoft Visual C++,请参阅 http://msdn.microsoft.com/en-us/library/hhzbb5c8(v=vs.80).aspx

一些编译器提供了方便的工具来确定特定预处理器宏的定义位置。参见,例如,How to know (in GCC) when given macro/preprocessor symbol gets declared?

关于c++ - #ifdef 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13240081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com