CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Redefining DEBUG

    I'm building a library that was originally written to be compiled with gcc (I'm using MSVC). Most of the functions look something like this:-

    Code:
    void the_function_name ()
    {
      assert ( <whatever> );
    
      if (DEBUG (BUFFER)) {
        // Dump some Debug information
      }
    
      // Rest of the function
    }
    I looked through the header files and discovered that one of them re-defines DEBUG - like so:-

    Code:
    #define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
    #define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0))
    If I build a Debug version I see the expected warning from MSVC:-

    warning C4005: 'DEBUG' : macro redefinition
    That in itself doesn't worry me unduly. What worries me more is that there's no guard around the above re-definition. So I'm worried in case DEBUG will now be defined for my Release builds as well as my Debug builds. I contacted the author who said that this won't happen because the compiler will optimize everything away when building a Release version. Does anyone know if that's true, either for gcc or MSVC?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Redefining DEBUG

    Can't you circumvent the name conflict problem by simply referring to _DEBUG rather than DEBUG in your own code?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Redefining DEBUG

    For my own code I probably could but I'm more concerned about existing header files (i.e. someone else's files) which might be conditionally including stuff, depending on whether DEBUG is or isn't defined. They probably won't have been written with the assumption that one particular module will define DEBUG - even when all the others don't.

    Of course, if the author's correct (in saying that the compiler itself will optimize his macros away) then I'm worrying unnecessarily - but I'm not really sure if that's true.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Redefining DEBUG

    Actually, http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx doesn't list DEBUG (and, to my surprise, not even NDEBUG), so apparently it's neither standard nor documented MS-specific. (_DEBUG is listed, but it's MS-specific and not compromised here at all.) I'd say 3rd party code making any assumptions about non-standard undocumented macros they didn't define themself rather is their problem...
    Last edited by Eri523; April 28th, 2013 at 10:26 AM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Redefining DEBUG

    Quote Originally Posted by John E View Post
    Of course, if the author's correct (in saying that the compiler itself will optimize his macros away) then I'm worrying unnecessarily - but I'm not really sure if that's true.
    As Eri523 pointed out, the author's mistake is to use names such as "DEBUG" for the macro. Using such a common name is begging for a name clash to occur somewhere.

    If anything, a name such as LIBRARYNAME_DEBUG, where "LIBRARYNAME" is the name of the library would have been better suited.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured