control #define directive
-
hi i want to control parts of my code with #ifdef directive,so I need to control a #define directive before! something like this:
if (condition)
#define activeand in other files I will check whether "active" is defined or not! ok,I can put :
#define active
in header file then use it in others cpp files but I cant use if statement in header! how can i do that? cheers, peiman
-
hi i want to control parts of my code with #ifdef directive,so I need to control a #define directive before! something like this:
if (condition)
#define activeand in other files I will check whether "active" is defined or not! ok,I can put :
#define active
in header file then use it in others cpp files but I cant use if statement in header! how can i do that? cheers, peiman
Please explain your problem using an example. It is (at least for me) unclear. Common practice is to define a value somewhere (inside a header file or even passing it to the compiler via command line; optionally by project settings). So assuming you use
ACTIVE
as boolean condition (defined or not defined):// General header file
// Comment this to undefine
#define ACTIVE// Any other file that has included above header file
#ifdef ACTION
// Do something
#endif#ifndef ACTION
// Do something else
#endifBut this won't work if there is a code condition like
if (condition)
Then you have to use similar code conditions in all other places. Note also that I have used all upper case for the defined value because this is common practice. To know about conditional inclusions using the preprocessor see Preprocessor directives - C++ Tutorials[^].
-
hi i want to control parts of my code with #ifdef directive,so I need to control a #define directive before! something like this:
if (condition)
#define activeand in other files I will check whether "active" is defined or not! ok,I can put :
#define active
in header file then use it in others cpp files but I cant use if statement in header! how can i do that? cheers, peiman
p3im4n wrote:
but I cant use if statement in header!
Why not? A header is just a text file that gets included into, and compiled as part of, some source code. It is quite common to do something like:
// in header file
#if defined(foo)
// some code to be generated
#endif// in cpp file
#define foo
#include