Warning about defined but not used for global variable

E

Eric Lilja

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, however, get the following warning
when building my program:
globals.hpp:12: warning: 'g_application_name' defined but not used

This warning is repeated for every file that includes globals.hpp, either
directly or indirectly.

However, if I change the type of g_application_name to const char * const,
the warning disappears.

So I guess that the proper way to define a global string variable that is
not to be written, only read, and should be shared for all source files is
to the define it with the type const char * const?

/ Eric
 
A

alexmdac

Yes you should use const char * const. const char * means that the
string buffer is writable but the pointer itself is constant. You
should treat string literals as read-only.
 
M

Matthias

Yes you should use const char * const. const char * means that the
string buffer is writable but the pointer itself is constant. You
should treat string literals as read-only.

Hm isn't it the other way around? AFAIK const char* means that the
character data is constant, but the pointer to it isn't, whereas char*
const means that the character data is variable and the pointer constant.
Correct me if I'm wrong.
 
S

scott urban

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";

Lose the static keyword; with static you get a separate variable named
'g_application_name' local to each translation unit that includes that
header - not 'global' at all. Change it to extern:

extern const char * g_application_name;

And, in one cpp file:

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, however, get the following warning
when building my program:
globals.hpp:12: warning: 'g_application_name' defined but not used

This warning is repeated for every file that includes globals.hpp, either
directly or indirectly.
However, if I change the type of g_application_name to const char * const,
the warning disappears.

I'm not sure what the rules are here - but for types like int:

const int name = 0;

in a header is used everywhere possibly in read only memory and doesn't
need to be set up like a global variable that might change. That
apparently applies to 'const char * const' too, at least for your
compiler.
So I guess that the proper way to define a global string variable that is
not to be written, only read, and should be shared for all source files is
to the define it with the type const char * const?

I think I would still do it the proper way, with a an extern declaration
in the header and a single definition in one cpp. At a minimum, if you
change the name of your application, you won't have to recompile all
files that include the header.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top