By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,771 Members | 1,709 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,771 IT Pros & Developers. It's quick & easy.

Warning: Defined but not used

P: n/a
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_child_class_name' defined but not
used
line 28 of globals.h is:
static const char* g_mdi_child_class_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
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
William Payne wrote:
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_child_class_name' defined but not
used
line 28 of globals.h is:
static const char* g_mdi_child_class_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.
...


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

Jul 22 '05 #2

P: n/a
William Payne wrote:
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_child_class_name' defined but not
used
line 28 of globals.h is:
static const char* g_mdi_child_class_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.


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_class_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


Jul 22 '05 #3

P: n/a

"Phlip" <ph*******@yahoo.com> wrote in message
news:Ed*******************@newssvr33.news.prodigy. com...
William Payne wrote:
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_child_class_name' defined but not used
line 28 of globals.h is:
static const char* g_mdi_child_class_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.
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_class_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


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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.