473,698 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 22 '05 #1
3 17401
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_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.
...


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_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.


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
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces


Jul 22 '05 #3

"Phlip" <ph*******@yaho o.com> wrote in message
news:Ed******** ***********@new ssvr33.news.pro digy.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_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.
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
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
6439
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 point of declaration of this variable, correct?): static const char * g_application_name = "Tiny, class-based MDI Example"; In another source file, I'm including the header globals.hpp and I'm using the variable g_application_name. I do,...
0
294
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 declaration SyntaxWarning: name 'layers' is used prior to global declaration
1
5801
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 compiler warning-level directives. I'm trying to find a way to silence the following warning displayed by Visual Studio 2005 for each file compiled: cl : Command line warning D9025 : overriding '/W2' with '/W3' One would think you could add...
13
5017
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
2006
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 this: warning: semantics of ">>" change in ANSI C; use explicit cast The statement to which this applies is: xuc = ((uc & 0xF0 ) >4);
1
5731
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 "RV2AJFRONT_NEW_private.h"
9
3211
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 anyone explain how to solve it? --------------------Configuration: PROJECT2 - Win32 Debug-------------------- Linking... RV2AJFRONT_NEW.obj : error LNK2005: _rtM_RV2AJFRONT_NEW already defined in RV2AJFRONT_NEW.obj RV2AJFRONT_NEW.obj : error...
13
9744
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 needed which causes the compiler to spit out a "helpful" warning. For example: % cat unused.c #include <stdio.h> int foo(char *str, void *data) {
3
2422
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 "hello world". #include <iostream> using namespace std;
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.