visual-studio C++ #define 预处理器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1399063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ #define preprocessor
提问by akif
I need to know that does the #define directive in C++ declares global label? By global I mean visible in every file?
我需要知道 C++ 中的 #define 指令是否声明了全局标签?全局我的意思是在每个文件中都可见?
I'm using Visual Studio 2008, (guess if that matters)
我正在使用 Visual Studio 2008,(猜猜这是否重要)
回答by Pieter
No, only in the current translation unit.
不,仅在当前翻译单元中。
I.e. every file which has #define, or includes a file that has the #definewill 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.
编辑,回应您的评论:在每个文件中获得一个定义,或者将它放在一个包含在任何地方的头文件中,或者使用一些编译器选项来添加定义。
e.g. 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++ 中指定这样的包含,但我确信以某种方式是可能的。
回答by Svetlozar Angelov
The #define directive substitutes token-string for all subsequent occurrences of an identifier in the source file. 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.
#define 指令将 token-string 替换为源文件中所有后续出现的标识符。标识符仅在形成令牌时才被替换。(请参阅 C++ 语言参考中的 C++ 标记。)例如,如果标识符出现在注释中、字符串中或作为较长标识符的一部分,则不会被替换。
It is not global, it is just for the current file/source
它不是全局的,它只是针对当前文件/源
回答by SadSido
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). 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...
由“#define”定义的符号从指令的位置到翻译单元的结尾都是可见的(或者到最近的“#undef”这个符号)。例如,如果你在“file.h”文件中定义了一些东西,这个符号会出现在“file.h”和每个文件中,包括“file.h”,直接或间接......
回答by MSalters
#defineworks 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. The /D command-line option allows you to pass the equivalent of #defines to the preprocessor, and you can put these in your property page of your project. That's almost "as global as it gets"; for multi-project solutions you can use shared inherited project property sheets.
但是,要使其真正全球化,您需要一个 Visual Studio 扩展。/D 命令行选项允许您将#defines的等效项传递给预处理器,您可以将它们放在项目的属性页中。这几乎是“尽可能地全球化”;对于多项目解决方案,您可以使用共享的继承项目属性表。
回答by ted_j
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.
您还可以设置由 .vsprops 文件支持的 Visual Studio 属性表,并将其分配给解决方案中的相关项目。
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
回答by Arkaitz Jimenez
It'll only be defined in the file you define it or in the files that include that file
它只会在您定义它的文件或包含该文件的文件中定义
回答by A. Murray
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"。
回答by Rowland Shaw
A #definedirective 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
要跨源文件定义某些内容,它需要位于所有源文件都包含的头文件中

