Thread: Preprocessing out function calls

  1. #1
    Registered User
    Join Date
    Feb 2009

    Unhappy Preprocessing out function calls


    I want to define a macro such that, when defined, it would be able to comment out a function (specially invocation - but then all for declaration, definition and invocation would be nice)

    For example,

    DIAG(a, b, c);

    would get transformed to:

    /* DIAG(a, b, c); */ (unlikely)


    /* DIAG(a, b, c); */;


    /* DIAG(a, b, c) */;

    .. etc

    The problem is, all C comments are replaced with single spaces by the preprocessor. (AFAIK - am I wrong?)

    Any ideas? The main point is to avoid the function invocations in certain runs rather than avoiding executing the body of the function (in that case, putting a return befor the first expression would have sufficed)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    You would be better off wrapping the contents of the function in #ifndef wrappings:
    void foo( ... )
    #ifndef BAZ
        ... function body here ...
    Naturally this prevents the use of it with standard library functions.

    Hope is the first step on the road to disappointment.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    I'm pretty sure you can't comment the function out as such.

    The common way to solve this problem is something like this:
    // Set WANT_DIAG to 1 when you want diagnostic output.
    #define WANT_DIAG 0
    #if WANT_DIAG
    #define DIAG(a, b, c)   ACTUAL_DIAG(a, b, c)
    #define DIAG(a, b, c)
    #if WANT_DIAG
    void ACTUAL_DIAG(int a, char *b, float c)
       .... // diagnostic code 
    int main()
       DIAG(11, "In main", 0.3);
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    The larch
    Join Date
    May 2006
    Can't you do something like this?

    //#define DEBUG
    #include <stdio.h>
    #ifdef DEBUG
    #define DIAGNOSE_CALL(x) x
    #define DIAGNOSE_CALL(x) (void*)0
    int main(void)
        int a = 42;
        DIAGNOSE_CALL(printf("[Conditionally printed] a = %d\n", a));
        printf("[Always printed] a = %d\n", a);
        return 0;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM