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