Link to home
Start Free TrialLog in
Avatar of bganoush
bganoush

asked on

Conditionally Preprocess out a Function call using #define...

I have some code I would like to preprocess right out of the object code conditionally...  The code in question is a call to a function that has a vararg... So far, I remember that I need to do something like this:

#define INCLUDE_THE_CODE

#ifdef INCLUDE_THE_CODE
  void TheCode(char* format, ...);
  #define MACRO(...) TheCode(...)
#else
  #define MACRO(...)
#endif

I want to be able to comment out the "#define INCLUDE_THE_CODE" and have the MACROS preprocessed out so that even the object for "TheCode" is not even included into the final object file... does that make sense?

Can anyone shed some light on this?

-- Bubba
ASKER CERTIFIED SOLUTION
Avatar of stefan73
stefan73
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What you have there appears to imply that your code "accesses" the relevant source by using a MACRO call. The lines listed will call the MACRO calls to be nulled out. That is, the calls to TheCode function will not be in the object if INCLUDE_THE_CODE is not set.

In order to remove the function itself from the objects, you will need to additionally surround the source for the function with an ifdef. Something like:

#ifdef INCLUDE_THE_CODE
void TheCode(char *fmt,...)
{     // code for this function
      ...
      ...
}
#endif
bganoush,

Errm, you weren't asking for varargs macros...
> I want to be able to comment out the "#define INCLUDE_THE_CODE" and
> have the MACROS preprocessed out so that even the object for "TheCode"
> is not even included into the final object file... does that make sense?

This makes a lot of sense for something like a trial version without save functionality.

You're probably better off using the #ifdefs not for your prototype (you can still link the code without them), but for the real implementation:

protos.h:
extern void TheCode(char* format,...);

the_code.c:
#ifdef INCLUDE_THE_CODE
void TheCode(char* format,...){
    do_something(...);
}
#else
void TheCode(char* format,...){}
#endif

By this, there is no real version of TheCode() function once you don't define INCLUDE_THE_CODE, just a dummy stub.

Stefan
bganoush,
Using a stub is better for large projects. You don't need to recompile every file which uses your TheCode() function, just the the_code.c file and a re-link will do.

The macro solution might perform slightly better, but only if it is called A LOT. If you know that you won't call TheCode zillions of times, use a stub.

Stefan
Avatar of bganoush
bganoush

ASKER


Thanks,

I WAS asking for a variable argument macro...  The only problem is that I don't think my compiler supports it. I have the c89 version of the AIX xlC compiler and even when I turn on the extended language level, it still gives me an error when it sees the ellipses (ie: three periods).

The documentation says to use the first syntax with the __VA_ARGS__ and it says that I have to turn on some option for the langage level but neither works.

In any case, that was the answer I was looking for. Thanks.

-- Bubba
bganoush,
I was a bit confused yesterday :-)

Stefan