Warning: Defined but not used

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Payne

    Warning: Defined but not used

    Hello, when compiling my program I get a warning in one of my header files
    (globals.h) each time a source file includes it.
    The warning reads:
    globals.h:28: warning: `const char*g_mdi_chil d_class_name' defined but not
    used
    line 28 of globals.h is:
    static const char* g_mdi_child_cla ss_name = "MDIChildClass" ;

    Why do I get this warning for this variable? It's used at three places
    throughout the program. I have two static const int variables in globals.h
    but I don't get any warnings regarding those. I also have five extern
    variables.

    / WP


  • Andrey Tarasevich

    #2
    Re: Warning: Defined but not used

    William Payne wrote:[color=blue]
    > Hello, when compiling my program I get a warning in one of my header files
    > (globals.h) each time a source file includes it.
    > The warning reads:
    > globals.h:28: warning: `const char*g_mdi_chil d_class_name' defined but not
    > used
    > line 28 of globals.h is:
    > static const char* g_mdi_child_cla ss_name = "MDIChildClass" ;
    >
    > Why do I get this warning for this variable? It's used at three places
    > throughout the program. I have two static const int variables in globals.h
    > but I don't get any warnings regarding those. I also have five extern
    > variables.
    > ...[/color]

    Since your variable is declared in a header file as 'static', each
    translation unit that includes that header file will get its own "copy"
    of this variable. Maybe in some of these translation units the variable
    is indeed not used, which causes the above warning to appear. It's just
    a guess though.

    --
    Best regards,
    Andrey Tarasevich

    Comment

    • Phlip

      #3
      Re: Warning: Defined but not used

      William Payne wrote:
      [color=blue]
      > Hello, when compiling my program I get a warning in one of my header files
      > (globals.h) each time a source file includes it.
      > The warning reads:
      > globals.h:28: warning: `const char*g_mdi_chil d_class_name' defined but not
      > used
      > line 28 of globals.h is:
      > static const char* g_mdi_child_cla ss_name = "MDIChildClass" ;
      >
      > Why do I get this warning for this variable? It's used at three places
      > throughout the program. I have two static const int variables in globals.h
      > but I don't get any warnings regarding those. I also have five extern
      > variables.[/color]

      Focus on the 'static'. That means each translation unit (.cpp file) gets its
      own copy of the variable (unless it's a compile-time constant, which it is
      not, because the 'const' is on the left side of the star *).

      So each of your translation units, except the three that use the variable,
      get a copy of it that they don't use.

      Your 'static const int' globals, by contrast, are compile-time constants, so
      the compiler logically unifies them, and doesn't grant each translation unit
      a separate one.

      Write this:

      static const char g_mdi_child_cla ss_name[] = "MDIChildClass" ;

      That declares intent better anyway. Nobody expected to re-point the former
      pointer.

      --
      Phlip





      Comment

      • William Payne

        #4
        Re: Warning: Defined but not used


        "Phlip" <phlip_cpp@yaho o.com> wrote in message
        news:EduZb.4148 3$O_6.23387@new ssvr33.news.pro digy.com...[color=blue]
        > William Payne wrote:
        >[color=green]
        > > Hello, when compiling my program I get a warning in one of my header[/color][/color]
        files[color=blue][color=green]
        > > (globals.h) each time a source file includes it.
        > > The warning reads:
        > > globals.h:28: warning: `const char*g_mdi_chil d_class_name' defined but[/color][/color]
        not[color=blue][color=green]
        > > used
        > > line 28 of globals.h is:
        > > static const char* g_mdi_child_cla ss_name = "MDIChildClass" ;
        > >
        > > Why do I get this warning for this variable? It's used at three places
        > > throughout the program. I have two static const int variables in[/color][/color]
        globals.h[color=blue][color=green]
        > > but I don't get any warnings regarding those. I also have five extern
        > > variables.[/color]
        >
        > Focus on the 'static'. That means each translation unit (.cpp file) gets[/color]
        its[color=blue]
        > own copy of the variable (unless it's a compile-time constant, which it is
        > not, because the 'const' is on the left side of the star *).
        >
        > So each of your translation units, except the three that use the variable,
        > get a copy of it that they don't use.
        >
        > Your 'static const int' globals, by contrast, are compile-time constants,[/color]
        so[color=blue]
        > the compiler logically unifies them, and doesn't grant each translation[/color]
        unit[color=blue]
        > a separate one.
        >
        > Write this:
        >
        > static const char g_mdi_child_cla ss_name[] = "MDIChildClass" ;
        >
        > That declares intent better anyway. Nobody expected to re-point the former
        > pointer.
        >
        > --
        > Phlip
        > http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
        >
        >
        >
        >[/color]

        Thanks Phlip, that made the warning disappear. Before I had declared those
        variables extern instead of static, but I made them static instead because
        my
        program does not change their values, they are simply used to make code
        clearer
        (I hate magic numbers/strings).
        Is this the proper way of declaring compile-time constants?

        / WP


        Comment

        Working...