Structs.h doesn't work if .h is included in more than one .cpp?
I want to use this callbacks object in more than one .cpp, but VS gives "one or more detected multiplicable characters" if I include more than once.
#ifndef HEADER_H
#define HEADER_H
typedef struct {
void(__cdecl *callbackOne)(bool val);
void(__cdecl *callbackTwo)(bool val);
void(__cdecl *callbackThree)(bool val);
} Callbacks;
Callbacks callbacks;
#endif
source to share
Callbacks callbacks;
This is the definition . Since it #include
works like a textual substitution, every source file that includes your header will have a (separate, different) instance of your structure.
Thus, when you merge the compiled object files, each one contains its own instance and corresponding symbol callbacks
, resulting in the described linker error.
In order to have one instance of your struct, you need to put the above definition in one source file.
To be able to use this instance from other source files, they must be able to reference it ("know its name"). What is the purpose of the ad ("give a name"):
extern Callbacks callbacks;
This is what you need to put in your title.
source to share