CppPilot

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

  1. A variable is defined in a header. Every .cpp file that includes the header defines it again. Declare it extern int counter; in the header and define it in one .cpp file, or make it inline int counter = 0; in C++17.
  2. A function is defined in a header without inline. Mark it inline, or keep only its declaration in the header and move its body to a .cpp file. Member functions defined inside the class are already inline.
  3. A .cpp file is included. #include "util.cpp" compiles that file twice. Include its header instead.
  4. Runtime library symbols are defined twice. Symbols already defined in LIBCMT.lib or MSVCRT.lib mean that libraries built with /MT and /MD are mixed: use the same runtime everywhere, as for LNK2038.
  5. 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.

Get CppPilot free Get started

Related errors: LNK2038, LNK2019. See all guides.