C++ Debug messages

This is a discussion on C++ Debug messages within the C++ Programming forums, part of the General Programming Boards category; Hi everybody. I was wondering if there is a way in C++ to have debug messages, and the ability to ...

  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
    32,851
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 05: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-25-2007, 12:07 AM
  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-25-2002, 12:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21