?
Solved

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

Posted on 2004-04-06
6
Medium Priority
?
351 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 12

Accepted Solution

by:
stefan73 earned 1000 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
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 recursion in the C programming language.
Suggested Courses

764 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