Using #define one time for multiple source files

為{幸葍}努か 提交于 2019-12-06 04:14:37

问题


Is there a way in Visual C++ to #define something in a cpp file and have it defined in other cpp files as well?


回答1:


There are at least two options:

  • Put the definition into a header file and include that header file in all the source files in which you need the definition
  • Use the /D compiler option to define the macro (this can also be set in the project properties under C/C++ -> Preprocessor -> Preprocessor Definitions)



回答2:


definitions.h:

#define foo bar

class.h:

#include "definitions.h"



回答3:


#define in a header (.h) file and #include that in all your .cpp files.




回答4:


Someone has mentioned the pre-processor setting in project properties already.

But you can also select only a few of the .cpp that you want the definition in and then right click on those and go to properties, then: C/C++ -> Preprocessor -> Preprocessor Definitions

It will define the defines for those .cpp files only and not your whole project.



来源:https://stackoverflow.com/questions/2946540/using-define-one-time-for-multiple-source-files

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