Thread: Conditional compilation and the preprocessor

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    47

    Conditional compilation and the preprocessor

    This questions is regarding conditional #define's. I was curious if I could use the pre-processor to figure out the value of a #define, or if in fact a #define is defined or undefined.

    example"

    Code:
    #ifdef ON
    #define size 10
    #else
    #define size 5
    #endif
    Can I find out if ON is defined in the above example and/or what the value of size is? Are there pre-processor directives for this? All i have used the pre-processor for is to do the passovers and remove all of this so I can analyze the actual code being used. This is alot different of an idea.

    Also, I am not talking about C code or runtime stuff, just basically a gcc directive I could use to say ON is defined, or size is 10 without any execution.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    You want to tell if ON is defined? So, what do you think "#ifdef ON" means?

    If you want to test the value of a preprocessor symbol, just do it:

    Code:
    #if size == 10
    ....
    #elif size == 5
    ....
    #endif
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The variables that are tested may not even be defined in any of the source code modules... They could be defined externally - in some programming whatchamacallit "envornment" bloated Microsoft Project/Visual-Outhouse or something.

    For example, for debugging you might have some conditional compiler switches surrounding diagnostic printfs all over the place. So depending on whether you chose to define some variable "DEBUG", the #ifdefs ensure that a diagnosing version is generated.

    Yeah, that's the way I do it, dagnabit.

Popular pages Recent additions subscribe to a feed