What really causes "Already defined in *.obj" error, and how to prevent it?

JWG 146 Reputation points
2023-06-10T16:21:42.04+00:00

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

Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,824 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,772 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.