C
C. J. Clegg
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?
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?