Thread: #ifdef DEBUG

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    159

    #ifdef DEBUG

    Hi,
    I was wondering in what case the following code defining ASSERT will be processed by gcc :
    Code:
    #ifdef DEBUG
    #define ASSERT(x) if(!(x)) { \
      std::cerr << "ASSERT FAILED IN " << __FILE__ << ":" << __LINE__ << endl; \
      abort(); \
    }
    #else
    #define ASSERT(x)
    #endif
    Is it when specifying -g to gcc?

    Is ASSERT defined as such same as assert() in assert.h?

    Thanks!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by lehe View Post
    Hi,
    I was wondering in what case the following code defining ASSERT will be processed by gcc :
    Whenever DEBUG is defined. gcc will never define it itself (even with -g). You must define it manually.

    The built-in assert will trigger by default, unless NDEBUG is defined. Again, the compiler DOES NOT DEFINE THIS FOR YOU. You must define it yourself when building for release.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm very much doubting that...
    Code:
    #include <stdio.h>
    
    int main()
    {
      #if DEBUG
      printf("Debug\n");
      #else
      printf("No Debug\n");
      #endif
      return 0;
    }
    compiled with gcc -g dbg.c, and then executing will show "No Debug" - at least using gcc-mingw 3.4.

    There is a symbol "NDEBUG" that is normally set by the compiler in "NO Debug" mode.

    --
    Mats
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    There is a symbol "NDEBUG" that is normally set by the compiler in "NO Debug" mode.
    I didn't think the compiler ever did that itself. I want to be able to enable asserts in release mode, or disable them for a debug build, if I want. Why should the compiler prevent me?

    And anyway, "release" and "debug" are just conveniences -- all that's actually real are the set of compiler switches. The -g flag means symbolic debugging information, not "build for debug." I can ask for optimization and debug at the same time if I want..
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, you are right, I'm wrong. I'm probably confusing this with the "regular build setup in Visual studio", which normally DOES set NDEBUG in the release mode.

    --
    Mats
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. makefiles - debug & release?
    By cpjust in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 04:00 PM
  2. #ifdef - Can It Be Used With Logic Such as OR / AND?
    By dedham_ma_man in forum C Programming
    Replies: 3
    Last Post: 04-21-2006, 02:57 PM
  3. Programming style: #ifdef vs regular if
    By hzmonte in forum C Programming
    Replies: 6
    Last Post: 11-03-2005, 12:47 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM