const int in header: linker error

F

Felix Kater

Hi,

I've got the header myheader.h:

#ifndef MY_HEADER
#define MY_HEADER
const int my_const_int = 99;
/* ... */
#endif

However, gcc says my_const_int was already defined were myheader.h is first included. If I define my_const_int without beeing a const or use #define, it works. What's the problem here?

Felix
 
U

Ulrich Eckhardt

Felix said:
I've got the header myheader.h:

#ifndef MY_HEADER
#define MY_HEADER
const int my_const_int = 99;
/* ... */
#endif

However, gcc says my_const_int was already defined were myheader.h
is first included.

Your include guard looks correct, so multiple inclusion should not lead to
multiple definition of this object while compiling. Please give the exact
error message.

OTOH, this will lead to multiple definitions when linking. Depending on
what you need, a declaration (with extern) or an object with internal
linkage (using file-scope static) is the solution.
If I define my_const_int without beeing a const or use #define, it
works.

If you use #define, that's pretty clear why, because that only affects the
preprocessor. Now, if you remove the 'const' and it works, something else
is happening there.

All this doesn't really make much sense to me, so please give some more
background info. For one thing, the handling of constants is different
between C and C++, so make sure that you don't use g++ but gcc to compile
and that you don't use some of the well-known extensions for C++
like .cpp, .C, .cc or .cxx.

Uli
 
P

pete

Felix said:
Hi,

I've got the header myheader.h:

#ifndef MY_HEADER
#define MY_HEADER
const int my_const_int = 99;
/* ... */
#endif

Don't define objects in header files. If more than one C file
#includes a header with an object definition,
then you'll have multiple instances of an object definition,
which is undefined code.

Make your external object definition in a C file.

If it isn't going to be static, then write
extern const int my_const_int;
in the header file.
 
O

Old Wolf

Felix said:
Hi,

I've got the header myheader.h:

#ifndef MY_HEADER
#define MY_HEADER
const int my_const_int = 99;
/* ... */
#endif

However, gcc says my_const_int was already defined were
myheader.h is first included. If I define my_const_int without
beeing a const or use #define, it works. What's the problem here?

I'm not exactly sure what you're describing; but you're not
allowed to have two external identifiers referring to different
areas of storage.

If you have two translation units that both include this header,
then you have that scenario.

With #define there is no storage so there is no problem.

If your int were not const then the problem still exists and the
code is still causing undefined behaviour (but GCC happens
to treat it 'correctly' in this case).

As well as pete's solution, a quick and dirty solution is to
mark your int as "static" too. Then it is not an external
identifier so there is no problem.

Another option is to use an enum:

enum {
my_const_int = 99;
};
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top