C++ #define preprocessor

自闭症网瘾萝莉.ら 提交于 2019-12-09 00:34:27

问题


I need to know that does the #define directive in C++ declares global label? By global I mean visible in every file?

I'm using Visual Studio 2008, (guess if that matters)


回答1:


No, only in the current translation unit.

I.e. every file which has #define, or includes a file that has the #define will see the definition.

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 -Dthedefine=itsvalue

Not sure how one specifies such includes in VC++, but I'm sure it's possible somehow.




回答2:


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.

It is not global, it is just for the current file/source

define in msdn




回答3:


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...




回答4:


#define works only in the translation unit it's defined in. With header files shared across translation units, that could be multiple files.

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.




回答5:


It'll only be defined in the file you define it or in the files that include that file




回答6:


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".




回答7:


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.

To define something across source files, it would need to be in a header file that all source files include




回答8:


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.

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



来源:https://stackoverflow.com/questions/1399063/c-define-preprocessor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!