MSVC build errors > LNK2005
LNK2005: symbol already defined
error LNK2005: "int counter" (?counter@@3HA) already defined in main.obj
The linker found two definitions of the same function or variable. C++ allows one definition per program; LNK1169 usually follows with the number of symbols defined more than once.
Causes and fixes
- A variable is defined in a header. Every
.cppfile that includes the header defines it again. Declare itextern int counter;in the header and define it in one.cppfile, or make itinline int counter = 0;in C++17. - A function is defined in a header without
inline. Mark itinline, or keep only its declaration in the header and move its body to a.cppfile. Member functions defined inside the class are already inline. - A
.cppfile is included.#include "util.cpp"compiles that file twice. Include its header instead. - Runtime library symbols are defined twice. Symbols already defined in
LIBCMT.liborMSVCRT.libmean that libraries built with/MTand/MDare mixed: use the same runtime everywhere, as for LNK2038. - Two libraries define the same symbol. Link only one of them, or rename the symbol in the code you control.
Fix LNK2005 with CppPilot
CppPilot reads the error, your code, and the project settings Visual Studio evaluates for the active configuration, then explains which of these causes applies. Choose Fix and the AI corrects it; CppPilot rebuilds the project and checks that the error is gone. Every change appears in Visual Studio for your approval.
Let the AI fix it in Visual Studio
CppPilot is a free extension that explains and fixes MSVC build errors with Codex, Claude, or CppPilot Cloud.
Related errors: LNK2038, LNK2019. See all guides.