Solved

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

Posted on 2004-04-06
6
330 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
ID: 10767557
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
ID: 10767584
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
ID: 10767622
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
Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

 
LVL 12

Expert Comment

by:stefan73
ID: 10767670
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
ID: 10768639

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
ID: 10773179
bganoush,
I was a bit confused yesterday :-)

Stefan
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
My eclipse editor won't start ? 4 144
libcurl - taking long time on curl_easy_perform 11 355
Problem to save 10 150
How to translate this 2-line while loop into C from Perl? 8 76
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

863 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

19 Experts available now in Live!

Get 1:1 Help Now