简体   繁体   English

C ++ #define预处理器

[英]C++ #define preprocessor

I need to know that does the #define directive in C++ declares global label? 我需要知道C ++中的#define指令是否声明了全局标签? By global I mean visible in every file? 全球我的意思是在每个文件中都可见?

I'm using Visual Studio 2008, (guess if that matters) 我正在使用Visual Studio 2008,(猜测是否重要)

No, only in the current translation unit. 不,只在当前的翻译单位。

Ie every file which has #define , or includes a file that has the #define will see the definition. 即每一个具有文件#define ,或者包含具有下一个文件#define将看到的定义。

Edit, to respond to your comment: to get a define in every file, either put it in a header which gets included everywhere, or use some compiler option to get defines added. 编辑,响应你的评论:在每个文件中获取一个定义,或者将它放在一个包含在任何地方的标题中,或者使用一些编译器选项来添加定义。

eg for gcc one would do 例如,对于gcc,人们会这样做

gcc -Dthedefine=itsvalue

Not sure how one specifies such includes in VC++, but I'm sure it's possible somehow. 不确定如何在VC ++中指定这样的包含,但我确信它可能以某种方式。

The #define directive substitutes token-string for all subsequent occurrences of an identifier in the source file. #define指令将token-string替换为源文件中所有后续出现的标识符。 The identifier is replaced only when it forms a token. 只有在形成令牌时才会替换标识符。 (See C++ Tokens in the C++ Language Reference.) For instance, identifier is not replaced if it appears in a comment, within a string, or as part of a longer identifier. (请参阅C ++语言参考中的C ++标记。)例如,如果标识符出现在注释中,字符串内或作为较长标识符的一部分,则不会替换标识符。

It is not global, it is just for the current file/source 它不是全局的,它只适用于当前的文件/源

define in msdn 在msdn中定义

Symbol, defined by "#define" is visible from the place of directive to the end of the translation unit (or to the nearest "#undef" for this symbol). 由“#define”定义的符号从指令的位置到翻译单元的末尾(或者对于该符号最近的“#undef”)是可见的。 For example, if you define something in "file.h" file, this symbol is seen in "file.h" and in every file, which includes "file.h", direct or indirect... 例如,如果在“file.h”文件中定义某些内容,则此符号可在“file.h”和每个文件中看到,其中包括“file.h”,直接或间接...

#define works only in the translation unit it's defined in. With header files shared across translation units, that could be multiple files. #define仅在其定义的翻译单元中起作用。在翻译单元之间共享头文件,可以是多个文件。

However, to make it truly global you need a Visual Studio extension. 但是,要使其真正全局化,您需要一个Visual Studio扩展。 The /D command-line option allows you to pass the equivalent of #define s to the preprocessor, and you can put these in your property page of your project. / D命令行选项允许您将等效的#define传递给预处理器,您可以将它们放在项目的属性页中。 That's almost "as global as it gets"; 这几乎是“全球化的”; for multi-project solutions you can use shared inherited project property sheets. 对于多项目解决方案,您可以使用共享的继承项目属性表。

它只能在您定义的文件或包含该文件的文件中定义

Whatever macro you have defined in one file will only be visible in another file if you have included the file with the macro in it, so it's not automatically global. 无论您在一个文件中定义了什么宏,只有在其中包含宏的文件时才会在另一个文件中显示,因此它不会自动为全局。 Generally, for this reason macros should be defined in your header files such that you can do a #include "headerFile.h". 通常,出于这个原因,应在头文件中定义宏,以便可以执行#include“headerFile.h”。

A #define directive will make the definition from that point in time, until compilation completes -- bear in mind that each .cpp counts as a separate compilation unit though. #define指令将从该时间点开始定义,直到编译完成为止 - 请记住,每个.cpp都算作一个单独的编译单元。

To define something across source files, it would need to be in a header file that all source files include 要在源文件中定义某些内容,它需要位于所有源文件包含的头文件中

You can also setup a visual studio property sheet, which is backed by a .vsprops file, and assign that to the relevant project(s) in your solution. 您还可以设置Visual Studio属性表,该表由.vsprops文件支持,并将其分配给解决方案中的相关项目。

This would allow you to easily maintain common settings of many types across multiple projects, even if they're in discrete solution files. 这将允许您轻松维护多个项目中多种类型的常用设置,即使它们位于离散的解决方案文件中。

http://msdn.microsoft.com/en-us/library/a4xbdz1e%28VS.80%29.aspx http://msdn.microsoft.com/en-us/library/a4xbdz1e%28VS.80%29.aspx

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

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