470,833 Members | 1,952 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

const int in header: linker error

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
May 11 '06 #1
3 3475
Felix Kater wrote:
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

May 11 '06 #2
Felix Kater wrote:

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.

--
pete
May 11 '06 #3
Felix Kater wrote:
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;
};

May 12 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Victor Hannak | last post: by
4 posts views Thread by cppsks | last post: by
15 posts views Thread by Dave | last post: by
12 posts views Thread by Riley DeWiley | last post: by
4 posts views Thread by C. J. Clegg | last post: by
4 posts views Thread by Rui.Hu719 | last post: by
36 posts views Thread by zouyongbin | last post: by
2 posts views Thread by Adrian | last post: by
10 posts views Thread by Stephen Howe | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.