Thread: C++ Debug messages

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    34

    C++ Debug messages

    Hi everybody. I was wondering if there is a way in C++ to have debug messages, and the ability to turn them off. For example, lets say I would like my program to have two modes. One mode is the mode where all the debug messages are printed, and another where they are not. Is it possible to do through the compiler? Ie to choose whether to compile the program with the debug info? I looked some stuff on google, and found some strange way in Visual C++. How about g++? Example would be great too. Thanks in advance.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Use the macro "NDEBUG" to indicate when something is a release build... so for example...

    Code:
    some_sort_function () {
    #ifndef NDEBUG
       // Only use the timer if it is a debug build
       TimingInformation timer ("some_sort_function()");
    #endif
       // Lots of code
    }
    Note that assert() changes behavior depending on whether NDEBUG is defined.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    34
    Thank you. So I can just do #define NDEBUG in my main file, and then all the code between #ifndef and #endif will be deactivated?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > So I can just do #define NDEBUG in my main file, and then all the code between #ifndef and #endif will be deactivated?
    It is the standard macro name for this kind of thing, so your IDE should automatically declare it in the release build.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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. Spy++ view messages posted/sent to a control, or from it?
    By hanhao in forum Windows Programming
    Replies: 2
    Last Post: 06-24-2007, 11:07 PM
  3. Sending windows messages
    By Ideswa in forum Windows Programming
    Replies: 2
    Last Post: 03-02-2006, 01:27 PM
  4. Ask about Debug Assert Failed
    By ooosawaddee3 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2002, 11:07 PM