The solution to this is to have a separate declaration and definition...
Header (*.h; sorry, I don't know WinAPI type names, adapt as necessary):
extern const char szClassName[];
Implementation (*.c or *.cpp)
const char szClassName[] = "hello, world"
You're seeing the problem because a new symbol szClassName
is being declared each time one of your *.c or *.cpp files includes the header (even with the include guards!); and that makes the linker confused (see below).
Do note that this will make sizeof(szClassName)
not work anymore.
Further explanation:
After preprocessing, the compiler is basically seeing this:
- file "a.c":
const char someSymbol[] = <some text, don't care what right now>;
- file "b.c":
const char someSymbol[] = <some text, don't care if it's the same>;
- file "c.c":
const char someSymbol[] = <some text, ditto>;
When the linker is linking the object files (say, "a.obj", "b.obj" and "c.obj"), it sees the same symbol being defined with a new value (at least as far as the linker is concerned) --- and thus it fails with an error.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…