473,320 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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_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
3 17206
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
0
by: chand | last post by:
Hi., In my api.py file 'g_opt_list' is defined globally g_opt_list =,,,,,,] when I run the py file, I am getting the Following Error SyntaxWarning: name 'g_opt_list' is used prior to global...
1
by: John Harris | last post by:
We have some C++ code that has a makefile which contains both /W2 and /W3. Due to the way the makefiles are written to be shared across multiple projects, it's not trival to eliminate the duplicate...
13
by: Kantha | last post by:
Hi all, I have declared an Union as follows typedef union { struct interrupt_bits { unsigned char c_int_hs_fs_status : 1, c_setup_intflag : 1,
8
by: Charles Sullivan | last post by:
I have a program written in C under Linux (gcc) which a user has ported to run under AT&T SysV R4. He sent me a copy of his makelog which displays a large number of compiler warnings similar to...
1
by: dewi | last post by:
Dear All, I am trying to compile a C code using Visual C++. Can anyone explain how to solve it? Thank You. #include <math.h> #include <string.h> #include "RV2AJFRONT_NEW.h" #include...
9
by: dewi | last post by:
Dear All, I have several problem about VC++. I succeed to convert Simulink MATLAB to C code using Real-Time Workshop. I am trying to compile a C code using Visual C++ and found the error. Can...
13
by: Rex Mottram | last post by:
I'm using an API which does a lot of callbacks. In classic callback style, each routine provides a void * pointer to carry user-defined data. Sometimes, however, the user-defined pointer is not...
3
by: Thomas Lenz | last post by:
The code below should allow to use a comma instead of << with ostreams and include a space between two operands when comma is used. e.g. cout << "hello", "world", endl; should print the line...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.