What really causes "Already defined in *.obj" error, and how to prevent it?
I would like to understand the real cause of this error, and best practice in preventing it.
I have an application App.cpp, which includes a main class - Main.h which Main.cpp includes from, but I have an Init.h for all the necessary system and other header file, which Main.cpp will include.
A code like this, in Init.h
constexpr auto MAX_LOADSTRING = 100;
WCHAR szTitle[MAX_LOADSTRING];
causes an error.
LNK 2005 wchart* szTitle(?szTitle@@3PA_WA) already defined in App.obj LNK1169 One or more multiply defined symbols found
I can make WCHAR static, and the error goes away
static WCHAR szTitle[MAX_LOADSTRING];
Or, I can move the code to App,cpp.
However, I don't think this is the solution, since, the error occurs on a simple variable like
bool test = false;
...and any other variable which isn't constant. I don't need these variables constant.
Also, it's more tidy and organized to keep all my global variables in a separate header file.
The message also says, see previous definitions of IDI_APP, which I noticed in the RC and Resource file, but I don't understand anything about that.
I'll like to understand what's relly happening, so that I can avoid running into the problem in the future. Thanks