简体   繁体   English

C#-#ifdef宏

[英]c++ - #ifdef macro

I can see 我可以看到

#ifdef <token>
code;
#endif

to be included, but I can't find it defined in any of the headers it includes. 包含在内,但我找不到它包含在任何标头中的定义。 Is there any other mechanism with which the token could be defined? 还有其他可以用来定义令牌的机制吗?

Firstly, there are macros that are implicitly defined by the compiler (for example, __cplusplus ). 首先,存在编译器隐式定义的宏(例如__cplusplus )。 Some of these are standard, and some are compiler-specific extensions. 其中有些是标准的,有些是编译器特定的扩展。 See your compiler manual for the full list. 有关完整列表,请参见编译器手册

Additionally, most compilers allow defining macros on the command line. 另外,大多数编译器允许在命令行上定义宏。 The exact mechanism is compiler-dependent, but often takes the form of a -D or /D command-line option. 确切的机制取决于编译器,但是通常采用-D/D命令行选项的形式。 For example, see the gcc manual : 例如,请参见gcc手册

-D name

Predefine name as a macro, with definition 1. 将名称预定义为宏,定义为1。

-D name=definition

The contents of definition are tokenized and processed as if they appeared during translation phase three in a `#define' directive. 定义的内容被标记和处理,就像它们出现在翻译的第三阶段中的#define指令中一样。 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. 如果要从Shell或类似Shell的程序中调用预处理器,则可能需要使用Shell的引号语法来保护字符,例如在Shell语法中具有含义的空格。

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. 括号对大多数shell有意义,因此您需要引用该选项。 With sh and csh, -D'name(args...)=definition' works. 使用sh和csh,-D'name(args ...)= definition'有效。

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

Some compilers provide convenient tools for figuring out where a particular preprocessor macro is defined. 一些编译器提供了方便的工具来确定在哪里定义了特定的预处理器宏。 See, for example, How to know (in GCC) when given macro/preprocessor symbol gets declared? 参见,例如, 如何知道(在GCC中)何时声明给定的宏/预处理器符号?

大多数(全部?)编译器都允许使用标志(在gcc中为-D)定义值,也有一些可由编译器本身设置。

Yes, of course, preprocessor directives can be set with the compiler. 是的,当然可以在编译器中设置预处理程序指令。 For example, gcc lets you add directives in the command line, you can specify directives in the project settings in Visual Studio. 例如,gcc允许您在命令行中添加指令,您可以在Visual Studio的项目设置中指定指令。 Also think about __cplusplus , or _LINE_ of _FILE_ . 还要考虑__cplusplus_LINE__FILE_ Those aren't defined anywhere, yet they exist. 这些没有在任何地方定义,但它们存在。 Also _DEBUG or UNICODE which are set up by the MSVS environment. 也是由MSVS环境设置的_DEBUGUNICODE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM