473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 3586
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Victor Hannak | last post by:
I have a class that needs to reference a const array in several of its methods. Where should I put the declaration/initialization of this array so that it is only created once when the class is...
4
by: cppsks | last post by:
I have been working on making a constant array available for the clients. However, it is placing it in the text segment, and external references result in unresolved references to the array. Taking...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
12
by: Riley DeWiley | last post by:
I am looking for a graceful way to declare a string const that is to be visible across many files. If I do this: //----hdr.h const char * sFoo = "foo"; //file.cpp
4
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
36
by: zouyongbin | last post by:
Stanley B Lippman in his "C++ Primer" that a definition like this should not appear in a header file: int ix; The inclusion of any of these definitions in two or more files of the same...
2
by: Adrian | last post by:
Hi, In a header file I tried const char *someval="this is a test"; which is included all over the place and I get linker errors about multiple defines. Why is this not folded when const char...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.