Preserved Topic: c++ custom header files (quick question) (Page 1 of 1) |
|
---|---|
Nervous Wreck (II) Inmate From: |
posted 01-17-2004 16:26
I have a header file with the following code: #ifndef ANTSHARE_H I need to include this file in 2 different files. As soon as I do that, I get "multiple definition of 'foo'" for every variable in antshare.h |
Bipolar (III) Inmate From: the uterus |
posted 01-17-2004 16:42
Global Variables and extern |
Nervous Wreck (II) Inmate From: |
posted 01-17-2004 17:11
Shouldn't code: extern "C++" Work? It doesn't, but prefixing every variable with "extern" doesn't work either... (then I get in function foo() undefined reference to bar) |
Bipolar (III) Inmate From: the uterus |
posted 01-17-2004 17:34
None of the variables in antshare.h need to be prefixed with 'extern', the variables need to be prefixed with extern when you want to use them within another file that includes the antshare.h. code: #ifndef SOME_FILE_THAT_INCLUDES_ANTSHARE_H |
Lunatic (VI) Mad Scientist From: Massachusetts, USA |
posted 01-17-2004 18:43
What you need to realize is that each .cpp file is compiled separately. The #ifdef stuff helps during each of these compilations so that the header file is not read more than once. However, the header file may be read while compiling a.cpp, and then again while compiling b.cpp, since a.cpp and b.cpp are compiled separately. |