Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "const int" and "const char*" in header (.h) files

Reply
Thread Tools

"const int" and "const char*" in header (.h) files

 
 
C. J. Clegg
Guest
Posts: n/a
 
      03-27-2006

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 definition
errors if the header file is #include'd in more than one code file.

The answer was that constants have internal linkage unless declared
extern, so it's OK.

So, you can put something like...

const int abc = 123;

.... in a header file and be fine (in C++, not in C).

I have run into a related problem.

In one of my header files I have:

const int maxLen = 128;

const char* theMsg = "Hello, world";

This header file is #include'd in about eleventy-gazillion places
throughout the system.

When I compile, the compilerand linker is perfectly happy with the
const int, but generates a slew of "multiple definition" errors at
link time for the const char*.

What's the difference between const int and const char* that would
make one work and the other not?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      03-27-2006
C. J. Clegg wrote:
> 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 definition
> errors if the header file is #include'd in more than one code file.
>
> The answer was that constants have internal linkage unless declared
> extern, so it's OK.
>

Do they?

> So, you can put something like...
>
> const int abc = 123;
>
> .... in a header file and be fine (in C++, not in C).
>

What happens if you take its address in more than one source file?

> I have run into a related problem.
>
> In one of my header files I have:
>
> const int maxLen = 128;
>
> const char* theMsg = "Hello, world";
>
> This header file is #include'd in about eleventy-gazillion places
> throughout the system.
>
> When I compile, the compilerand linker is perfectly happy with the
> const int, but generates a slew of "multiple definition" errors at
> link time for the const char*.
>
> What's the difference between const int and const char* that would
> make one work and the other not?


My guess is that the const int does not require a memory location,
unless you take its address.

The const char* is a string literal that will be duplicated.

I always use extern for constant declarations, with the definition in a
appropriate source file.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      03-27-2006
In article <(E-Mail Removed)>,
C. J. Clegg <(E-Mail Removed)> wrote:

> 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 definition
> errors if the header file is #include'd in more than one code file.
>
> The answer was that constants have internal linkage unless declared
> extern, so it's OK.
>
> So, you can put something like...
>
> const int abc = 123;
>
> ... in a header file and be fine (in C++, not in C).
>
> I have run into a related problem.
>
> In one of my header files I have:
>
> const int maxLen = 128;
>
> const char* theMsg = "Hello, world";


Try this:

const char* const theMsg = "Hello, world";

See if that gets rid of the errors.

> This header file is #include'd in about eleventy-gazillion places
> throughout the system.
>
> When I compile, the compilerand linker is perfectly happy with the
> const int, but generates a slew of "multiple definition" errors at
> link time for the const char*.
>
> What's the difference between const int and const char* that would
> make one work and the other not?


The difference is that "const int" is const, whereas "const char*" is
mutable (even if that to which it points is not.)


--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      03-28-2006
Daniel T. wrote:
>>
>> I have run into a related problem.
>>
>> In one of my header files I have:
>>
>> const int maxLen = 128;
>>
>> const char* theMsg = "Hello, world";

>
> Try this:
>
> const char* const theMsg = "Hello, world";
>
> See if that gets rid of the errors.
>


That should. In the OP's version, theMsg isn't a const, it just points
to const data, and therefore theMsg has external linkage.

In Daniel's version, in addition to pointing at const data, the pointer
itself is a const, and therefore should have internal linkage.
 
Reply With Quote
 
C. J. Clegg
Guest
Posts: n/a
 
      03-28-2006
On Mon, 27 Mar 2006 23:46:32 GMT, "Daniel T." <(E-Mail Removed)>
wrote:

>Try this:
>
>const char* const theMsg = "Hello, world";
>
>See if that gets rid of the errors.


Yup, that did it, thanks!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Header files with "header.h" or <header.h> ?? mlt C++ 2 01-31-2009 02:54 PM
Search for built-in header and application header linq936@gmail.com C Programming 3 08-25-2007 11:49 PM
UNIX header files to Windows header files anand.ba@gmail.com C Programming 3 05-01-2006 03:57 PM
Header files included in header files John Smith C Programming 18 07-24-2004 04:55 AM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM



Advertisments