I use #pragma once
often and it seems to work fine when dealing with headers and but for some reason, the following creates the linker error of multiple definitions:
#pragma once
int someVariable=5;
Shouldn't the pragma
prevent this too?
I use #pragma once
often and it seems to work fine when dealing with headers and but for some reason, the following creates the linker error of multiple definitions:
#pragma once
int someVariable=5;
Shouldn't the pragma
prevent this too?
Shouldn't the pragma prevent this too?
No, in this case multiple definitions of someVariable will be created if this header file is included in multiple places. If B.h and C.h both include your head file, then two someVariable will be created.
Better way is to define variables in only one .cpp file and use extern
in other places.