Solved

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

Posted on 2004-04-06
6
333 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c language help - file paths 7 147
Trouble linking program with -lcrypt 3 149
List out all word 7 292
If a cell in a range equals "YES" return specific Test. 4 31
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

773 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