#ifndef _DEBUG equivalent...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Bass

    #1

    #ifndef _DEBUG equivalent...

    In C++ you can use the following:

    #ifndef _DEBUG

    // code here only runs in debug mode

    #endif

    where you can set different code to be included in different builds. Useful
    for having a logging system for debugging only when compiling the project in
    Debug, but wanting to turn it off in Release.

    It this possible with c#.net? (VS.net 2003)

    Thanks!
    Dan.


  • Serge

    #2
    Re: #ifndef _DEBUG equivalent...

    #if DEBUG

    // code here only runs in debug mode


    #endif

    "Daniel Bass" <I'm really @ sick of spam> wrote in message
    news:eMbze5nrDH A.2408@tk2msftn gp13.phx.gbl...[color=blue]
    > In C++ you can use the following:
    >
    > #ifndef _DEBUG
    >
    > // code here only runs in debug mode
    >
    > #endif
    >
    > where you can set different code to be included in different builds.[/color]
    Useful[color=blue]
    > for having a logging system for debugging only when compiling the project[/color]
    in[color=blue]
    > Debug, but wanting to turn it off in Release.
    >
    > It this possible with c#.net? (VS.net 2003)
    >
    > Thanks!
    > Dan.
    >
    >[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: #ifndef _DEBUG equivalent...

      <"Daniel Bass" <I'm really @ sick of spam>> wrote:[color=blue]
      > In C++ you can use the following:
      >
      > #ifndef _DEBUG
      >
      > // code here only runs in debug mode
      >
      > #endif[/color]

      I think you meant:

      #ifdef _DEBUG

      there.

      You can do that with C# too:

      #if DEBUG
      ....
      #endif
      [color=blue]
      > where you can set different code to be included in different builds. Useful
      > for having a logging system for debugging only when compiling the project in
      > Debug, but wanting to turn it off in Release.[/color]

      For that kind of thing it's actually easier to use an attribute on the
      method you're calling - look at the Trace class for an example.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Eric Newton

        #4
        Re: #ifndef _DEBUG equivalent...

        Also, if you use the Debug statements "Debug.WriteLin e", "Debug.*", the c#
        and vb.net compilers automatically ignore these statements in release
        builds.

        --
        Eric Newton
        C#/ASP Application Developer

        eric@cc.ensoft-software.com [remove the first "CC."]

        "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
        news:MPG.1a254d 229a290dbd989ad c@msnews.micros oft.com...[color=blue]
        > <"Daniel Bass" <I'm really @ sick of spam>> wrote:[color=green]
        > > In C++ you can use the following:
        > >
        > > #ifndef _DEBUG
        > >
        > > // code here only runs in debug mode
        > >
        > > #endif[/color]
        >
        > I think you meant:
        >
        > #ifdef _DEBUG
        >
        > there.
        >
        > You can do that with C# too:
        >
        > #if DEBUG
        > ...
        > #endif
        >[color=green]
        > > where you can set different code to be included in different builds.[/color][/color]
        Useful[color=blue][color=green]
        > > for having a logging system for debugging only when compiling the[/color][/color]
        project in[color=blue][color=green]
        > > Debug, but wanting to turn it off in Release.[/color]
        >
        > For that kind of thing it's actually easier to use an attribute on the
        > method you're calling - look at the Trace class for an example.
        >
        > --
        > Jon Skeet - <skeet@pobox.co m>
        > http://www.pobox.com/~skeet
        > If replying to the group, please do not mail me too[/color]


        Comment

        • Ed Courtenay

          #5
          Re: #ifndef _DEBUG equivalent...


          That's not exactly true; the Debug.Write statement will be compiled and
          called in both Debug and Release builds. The difference is that in Release
          builds the function won't actually do anything as TraceInternal class won't
          have any listeners registered.

          This means you will still have a (slight) overhead in Release builds
          whenever Debug.Write is called.

          "Eric Newton" <eric@cc.enso ft-software.com> wrote in message
          news:ud466qtrDH A.540@tk2msftng p13.phx.gbl...[color=blue]
          > Also, if you use the Debug statements "Debug.WriteLin e", "Debug.*", the c#
          > and vb.net compilers automatically ignore these statements in release
          > builds.
          >
          > --
          > Eric Newton
          > C#/ASP Application Developer
          > http://ensoft-software.com/
          > eric@cc.ensoft-software.com [remove the first "CC."]
          >
          > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          > news:MPG.1a254d 229a290dbd989ad c@msnews.micros oft.com...[color=green]
          > > <"Daniel Bass" <I'm really @ sick of spam>> wrote:[color=darkred]
          > > > In C++ you can use the following:
          > > >
          > > > #ifndef _DEBUG
          > > >
          > > > // code here only runs in debug mode
          > > >
          > > > #endif[/color]
          > >
          > > I think you meant:
          > >
          > > #ifdef _DEBUG
          > >
          > > there.
          > >
          > > You can do that with C# too:
          > >
          > > #if DEBUG
          > > ...
          > > #endif
          > >[color=darkred]
          > > > where you can set different code to be included in different builds.[/color][/color]
          > Useful[color=green][color=darkred]
          > > > for having a logging system for debugging only when compiling the[/color][/color]
          > project in[color=green][color=darkred]
          > > > Debug, but wanting to turn it off in Release.[/color]
          > >
          > > For that kind of thing it's actually easier to use an attribute on the
          > > method you're calling - look at the Trace class for an example.
          > >
          > > --
          > > Jon Skeet - <skeet@pobox.co m>
          > > http://www.pobox.com/~skeet
          > > If replying to the group, please do not mail me too[/color]
          >
          >[/color]


          Comment

          • Daniel Bass

            #6
            Re: #ifndef _DEBUG equivalent...

            Will it? I mean, the optimiser in the compiler in release mode will say that
            the procedure call is empty, and has no effect, therefore the call is
            altogether elliminated...

            "Ed Courtenay" <my-first-name@edcourtena y.co.uk> wrote in message
            news:O9c3hTCsDH A.1512@TK2MSFTN GP10.phx.gbl...

            That's not exactly true; the Debug.Write statement will be compiled and
            called in both Debug and Release builds. The difference is that in Release
            builds the function won't actually do anything as TraceInternal class won't
            have any listeners registered.

            This means you will still have a (slight) overhead in Release builds
            whenever Debug.Write is called.

            "Eric Newton" <eric@cc.enso ft-software.com> wrote in message
            news:ud466qtrDH A.540@tk2msftng p13.phx.gbl...[color=blue]
            > Also, if you use the Debug statements "Debug.WriteLin e", "Debug.*", the c#
            > and vb.net compilers automatically ignore these statements in release
            > builds.
            >
            > --
            > Eric Newton
            > C#/ASP Application Developer
            > http://ensoft-software.com/
            > eric@cc.ensoft-software.com [remove the first "CC."]
            >
            > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            > news:MPG.1a254d 229a290dbd989ad c@msnews.micros oft.com...[color=green]
            > > <"Daniel Bass" <I'm really @ sick of spam>> wrote:[color=darkred]
            > > > In C++ you can use the following:
            > > >
            > > > #ifndef _DEBUG
            > > >
            > > > // code here only runs in debug mode
            > > >
            > > > #endif[/color]
            > >
            > > I think you meant:
            > >
            > > #ifdef _DEBUG
            > >
            > > there.
            > >
            > > You can do that with C# too:
            > >
            > > #if DEBUG
            > > ...
            > > #endif
            > >[color=darkred]
            > > > where you can set different code to be included in different builds.[/color][/color]
            > Useful[color=green][color=darkred]
            > > > for having a logging system for debugging only when compiling the[/color][/color]
            > project in[color=green][color=darkred]
            > > > Debug, but wanting to turn it off in Release.[/color]
            > >
            > > For that kind of thing it's actually easier to use an attribute on the
            > > method you're calling - look at the Trace class for an example.
            > >
            > > --
            > > Jon Skeet - <skeet@pobox.co m>
            > > http://www.pobox.com/~skeet
            > > If replying to the group, please do not mail me too[/color]
            >
            >[/color]



            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: #ifndef _DEBUG equivalent...

              Ed Courtenay <my-first-name@edcourtena y.co.uk> wrote:[color=blue]
              > That's not exactly true; the Debug.Write statement will be compiled and
              > called in both Debug and Release builds.[/color]

              No it won't. Here's a sample program:

              using System;
              using System.Diagnost ics;

              class Test
              {
              static void Main(string[] args)
              {
              Debug.WriteLine ("Foo");
              }
              }

              Compiled with

              csc Test.cs

              the IL for Main is:

              ..method private hidebysig static void Main(string[] args) cil managed
              {
              .entrypoint
              // Code size 1 (0x1)
              .maxstack 0
              IL_0000: ret
              } // end of method Test::Main


              Compiled with

              csc /D:DEBUG Test.cs

              the IL is:

              ..method private hidebysig static void Main(string[] args) cil managed
              {
              .entrypoint
              // Code size 11 (0xb)
              .maxstack 1
              IL_0000: ldstr "Foo"
              IL_0005: call void [System]System.Diagnost ics.Debug::Writ eLine
              (string)
              IL_000a: ret
              } // end of method Test::Main


              Note the lack of a call in the version where the DEBUG symbol isn't
              defined.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              • Ed Courtenay

                #8
                Re: #ifndef _DEBUG equivalent...


                My apologies - from looking through the Debug class with Lutz Roeder's
                excellent Reflector, it seems that methods like Debug.Write are marked with
                a [Conditional("DE BUG")] attribute. I guess that if the compiler sees this
                attribute on a method it drops it from the IL when compiled without the
                DEBUG flag.

                Is this correct?

                "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                news:MPG.1a281e edd54fff0f989b0 c@msnews.micros oft.com...[color=blue]
                > Ed Courtenay <my-first-name@edcourtena y.co.uk> wrote:[color=green]
                > > That's not exactly true; the Debug.Write statement will be compiled and
                > > called in both Debug and Release builds.[/color]
                >
                > No it won't. Here's a sample program:
                >
                > using System;
                > using System.Diagnost ics;
                >
                > class Test
                > {
                > static void Main(string[] args)
                > {
                > Debug.WriteLine ("Foo");
                > }
                > }
                >
                > Compiled with
                >
                > csc Test.cs
                >
                > the IL for Main is:
                >
                > .method private hidebysig static void Main(string[] args) cil managed
                > {
                > .entrypoint
                > // Code size 1 (0x1)
                > .maxstack 0
                > IL_0000: ret
                > } // end of method Test::Main
                >
                >
                > Compiled with
                >
                > csc /D:DEBUG Test.cs
                >
                > the IL is:
                >
                > .method private hidebysig static void Main(string[] args) cil managed
                > {
                > .entrypoint
                > // Code size 11 (0xb)
                > .maxstack 1
                > IL_0000: ldstr "Foo"
                > IL_0005: call void [System]System.Diagnost ics.Debug::Writ eLine
                > (string)
                > IL_000a: ret
                > } // end of method Test::Main
                >
                >
                > Note the lack of a call in the version where the DEBUG symbol isn't
                > defined.
                >
                > --
                > Jon Skeet - <skeet@pobox.co m>
                > http://www.pobox.com/~skeet
                > If replying to the group, please do not mail me too[/color]


                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: #ifndef _DEBUG equivalent...

                  Ed Courtenay <my-first-name@edcourtena y.co.uk> wrote:[color=blue]
                  > My apologies - from looking through the Debug class with Lutz Roeder's
                  > excellent Reflector, it seems that methods like Debug.Write are marked with
                  > a [Conditional("DE BUG")] attribute. I guess that if the compiler sees this
                  > attribute on a method it drops it from the IL when compiled without the
                  > DEBUG flag.
                  >
                  > Is this correct?[/color]

                  Yes, that's exactly correct.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • Eric Newton

                    #10
                    Re: #ifndef _DEBUG equivalent...

                    Whew, I was nervous when Daniel had said that the call was still being
                    made... thinking about all the Debug.WriteLine 's that I tend to do...!

                    --
                    Eric Newton
                    C#/ASP Application Developer

                    eric@cc.ensoft-software.com [remove the first "CC."]

                    "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                    news:MPG.1a2853 7e64fc7f79989b1 2@msnews.micros oft.com...[color=blue]
                    > Ed Courtenay <my-first-name@edcourtena y.co.uk> wrote:[color=green]
                    > > My apologies - from looking through the Debug class with Lutz Roeder's
                    > > excellent Reflector, it seems that methods like Debug.Write are marked[/color][/color]
                    with[color=blue][color=green]
                    > > a [Conditional("DE BUG")] attribute. I guess that if the compiler sees[/color][/color]
                    this[color=blue][color=green]
                    > > attribute on a method it drops it from the IL when compiled without the
                    > > DEBUG flag.
                    > >
                    > > Is this correct?[/color]
                    >
                    > Yes, that's exactly correct.
                    >
                    > --
                    > Jon Skeet - <skeet@pobox.co m>
                    > http://www.pobox.com/~skeet
                    > If replying to the group, please do not mail me too[/color]


                    Comment

                    • Daniel Bass

                      #11
                      Re: #ifndef _DEBUG equivalent...

                      I never said such a thing, 'twas not me sir. ;o)

                      I'm in the same prediciment with calls to the debugger framework i've got
                      setup.


                      "Eric Newton" <eric@cc.enso ft-software.com> wrote in message
                      news:uFmpHRauDH A.2180@TK2MSFTN GP09.phx.gbl...
                      Whew, I was nervous when Daniel had said that the call was still being
                      made... thinking about all the Debug.WriteLine 's that I tend to do...!

                      --
                      Eric Newton
                      C#/ASP Application Developer

                      eric@cc.ensoft-software.com [remove the first "CC."]

                      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                      news:MPG.1a2853 7e64fc7f79989b1 2@msnews.micros oft.com...[color=blue]
                      > Ed Courtenay <my-first-name@edcourtena y.co.uk> wrote:[color=green]
                      > > My apologies - from looking through the Debug class with Lutz Roeder's
                      > > excellent Reflector, it seems that methods like Debug.Write are marked[/color][/color]
                      with[color=blue][color=green]
                      > > a [Conditional("DE BUG")] attribute. I guess that if the compiler sees[/color][/color]
                      this[color=blue][color=green]
                      > > attribute on a method it drops it from the IL when compiled without the
                      > > DEBUG flag.
                      > >
                      > > Is this correct?[/color]
                      >
                      > Yes, that's exactly correct.
                      >
                      > --
                      > Jon Skeet - <skeet@pobox.co m>
                      > http://www.pobox.com/~skeet
                      > If replying to the group, please do not mail me too[/color]



                      Comment

                      Working...