2008-04-07 17:31:53 UTC
I was always under the impression that if I created a header file that contained:
#ifndef HEADER_H_
#define HEADER_H_
const char* foo = "Hello" ;
#endif
I could safely include this header in as many .cpp files as I wanted, and the preprocessor directives would prevent multiple inclusions?
I'm using g++, and getting "multiple definition" errors on foo when I use the above practice. I'm now assuming that the preprocessor is applied once per .cpp file -- meaning each file will only define up to one instance of foo, however, the linker will freak out when it goes to work.
What is the best way to handle this problem? I know I could just define foo in the .cpp file, but its a constant and I like to keep constants in the .h file. I've thought about making foo extern in the header, but then I still have to initalize it in the .cpp file -- How can I do this cleanly so that the initialization is done in the header?
Thanks!