Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 27th, 2006, 09:45 PM
C. J. Clegg
Guest
 
Posts: n/a
Default "const int" and "const char*" in header (.h) files


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?
  #2  
Old March 27th, 2006, 11:05 PM
Ian Collins
Guest
 
Posts: n/a
Default Re: "const int" and "const char*" in header (.h) files

C. J. Clegg wrote:[color=blue]
> 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.
>[/color]
Do they?
[color=blue]
> So, you can put something like...
>
> const int abc = 123;
>
> .... in a header file and be fine (in C++, not in C).
>[/color]
What happens if you take its address in more than one source file?
[color=blue]
> 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?[/color]

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.
  #3  
Old March 27th, 2006, 11:55 PM
Daniel T.
Guest
 
Posts: n/a
Default Re: "const int" and "const char*" in header (.h) files

In article <29lg22tlr1r0h66saihn6b2j4edir60mfe@4ax.com>,
C. J. Clegg <reply.to.newsgroup@nospam.no> wrote:
[color=blue]
> 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";[/color]

Try this:

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

See if that gets rid of the errors.
[color=blue]
> 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?[/color]

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.
  #4  
Old March 28th, 2006, 12:35 AM
red floyd
Guest
 
Posts: n/a
Default Re: "const int" and "const char*" in header (.h) files

Daniel T. wrote:[color=blue][color=green]
>>
>> I have run into a related problem.
>>
>> In one of my header files I have:
>>
>> const int maxLen = 128;
>>
>> const char* theMsg = "Hello, world";[/color]
>
> Try this:
>
> const char* const theMsg = "Hello, world";
>
> See if that gets rid of the errors.
>[/color]

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.
  #5  
Old March 28th, 2006, 12:25 PM
C. J. Clegg
Guest
 
Posts: n/a
Default Re: "const int" and "const char*" in header (.h) files

On Mon, 27 Mar 2006 23:46:32 GMT, "Daniel T." <postmaster@verizon.net>
wrote:
[color=blue]
>Try this:
>
>const char* const theMsg = "Hello, world";
>
>See if that gets rid of the errors.[/color]

Yup, that did it, thanks! :-)
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles