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

Discussion in 'C++' started by C. J. Clegg, Mar 27, 2006.

  1. C. J. Clegg

    C. J. Clegg Guest

    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?
     
    C. J. Clegg, Mar 27, 2006
    #1
    1. Advertising

  2. C. J. Clegg

    Ian Collins Guest

    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.
     
    Ian Collins, Mar 27, 2006
    #2
    1. Advertising

  3. C. J. Clegg

    Daniel T. Guest

    In article <>,
    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.
    >
    > 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.
     
    Daniel T., Mar 28, 2006
    #3
  4. C. J. Clegg

    red floyd Guest

    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.
     
    red floyd, Mar 28, 2006
    #4
  5. C. J. Clegg

    C. J. Clegg Guest

    On Mon, 27 Mar 2006 23:46:32 GMT, "Daniel T." <>
    wrote:

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


    Yup, that did it, thanks! :)
     
    C. J. Clegg, Mar 28, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Santa
    Replies:
    1
    Views:
    1,157
    Mark A. Odell
    Jul 17, 2003
  2. Replies:
    24
    Views:
    904
    Netocrat
    Oct 30, 2005
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,151
    Ian Collins
    May 9, 2006
  4. Gary
    Replies:
    9
    Views:
    1,526
    CBFalconer
    Aug 24, 2006
  5. Replies:
    11
    Views:
    1,151
Loading...

Share This Page