Solved

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

Posted on 2004-04-06
6
328 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:bganoush
  • 4
6 Comments
 
LVL 12

Accepted Solution

by:
stefan73 earned 250 total points
Comment Utility
Hi bganoush,
Straightforward: Won't work with VC++. But if you have a modern, ISO99-compliant compiler (such as gcc), you can use macros with a variable argument number:

#define debug(format, ...) fprintf (stderr, format, __VA_ARGS__)
(ISO99 standard)

#define debug(format, args...) fprintf (stderr, format, args)
(gcc-specific)

More details on
http://gcc.gnu.org/onlinedocs/gcc-3.3.3/gcc/Variadic-Macros.html#Variadic%20Macros


Cheers,
Stefan
0
 
LVL 16

Expert Comment

by:imladris
Comment Utility
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
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
0
 

Author Comment

by:bganoush
Comment Utility

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
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
bganoush,
I was a bit confused yesterday :-)

Stefan
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now