is there a way in visual c++ to #define something in a cpp file and have it defined in other cpp files as well?
+9
A:
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)
James McNellis
2010-05-31 23:53:10
+1 for the compiler flag
egrunin
2010-05-31 23:56:35
+1
A:
#define in a header (.h) file and #include that in all your .cpp files.
Matti Virkkunen
2010-05-31 23:53:27
@ebasconp: Include guards aren't strictly necessary in this case since macros can be redefined so long as the new definition is identical to the previous definition (if you include the header twice, both definitions will obviously be identical).
James McNellis
2010-06-01 02:09:45
A:
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.
Brian R. Bondy
2010-06-01 06:42:55