Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Activate print messages on debug mode / Disable print statements on release mode in C++ linux

    C++ Gurus
    c++ linux debug print
    3
    7
    6193
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K
      kumararajas last edited by kumararajas

      Hello!

      I am looking for a best way to enable / disable the print messages in my application.

      In my application, to help the debugging, I have std::cout's to print the messages.

      In the release mode, I don't want to print them, as it is a overhead. However, I want to have them in debug mode.

      What is the best way to handle this?

      Suggestion much appreciated!

      Thanks,
      Kumara

      --Kumar

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        This sort of stuff is usually a job for preprocessor.
        One way would be to simply wrap the prints in #ifdefs. This is a bit intrusive and makes code harder to read so a little wrapper could help here:

        #ifdef _DEBUG
        #define PRINTSTUFF(stuff) std::cout << (stuff);
        #else
        #define PRINTSTUFF(stuff)
        #endif
        
        //and then just:
        PRINTSTUFF("stuff")
        

        This will expand to nothing in release mode so no overhead will be present.

        1 Reply Last reply Reply Quote 0
        • K
          kumararajas last edited by

          This is excellent! Thank you!

          To be clear, _DEBUG macro is defined by whom? Operating system? Is it available for all the operating systems?

          Please help me out.

          Thanks,
          Kumara

          --Kumar

          1 Reply Last reply Reply Quote 0
          • Chris Kawa
            Chris Kawa Moderators last edited by Chris Kawa

            This was just an example and no ultimate solution. No, it has nothing to do with the OS. It's a compiler/toolchain specific thing. Some compilers (e.g. GCC) don't have a clear notion of a release/debug. They just have a bunch of flags like optimization and debug info generation to mix and match. In such cases it's just a toolchain preset of what a debug build means.

            The above _DEBUG define is specific to MSVC. Other compilers use DEBUG, NDEBUG, other stuff or nothing at all. Unfortunately there's no standard that would define that. The only way to make sure is to define something yourself. For example if you're using qmake you could add something like this in the .pro file:

            debug {
              DEFINES += DEBUG_BUILD
            }
            

            and then test for DEBUG_BUILDin the source code.

            1 Reply Last reply Reply Quote 0
            • K
              kumararajas last edited by

              Very clear explanation! Thank you again!

              In case of qmake, it is pretty clear.

              How about in case of a plain c++ code with a Makefile? Any suggestions please?

              (I am loving the discussion. It is helping to understand lot. Thank you)

              --Kumar

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                Hi,

                You would create two different targets where you setup these flags then call e.g. make release

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply Reply Quote 0
                • K
                  kumararajas last edited by kumararajas

                  Thanks Sam for Chris for great ideas.

                  I had put together made an test implementation.

                  I wrote a Makefile as

                  release: main.cpp
                  	g++ main.cpp -o booh
                  
                  debug: main.cpp
                  	g++ main.cpp -o booh -DDEBUG_ENABLE
                  

                  Since release configuration at the top, that becomes default one. So, here in this case, release is by default.

                  And, when someone wants to enable debug messages, they would call "make debug", which defines a MACRO called DEBUG_ENABLE.

                  In the .cpp file,

                  #ifdef DEBUG_ENABLE
                      #define PRINTDEBUG(debugString) std::cout << (debugString) << std::endl;
                  #else
                      #define PRINTDEBUG(debugString)
                  #endif
                  

                  So, based on configuration, Macro is either enabled or disabled.

                  Thanks!

                  --Kumar

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post