Arduino build error

multiple definition of 'counter'

A name that can be defined only once was defined twice. The compiler reports it as redefinition of when both are in one file, and the linker reports it as multiple definition of when they are in two files.

  • Build error
  • 3 common causes
  • Seen 354 times in failed builds on Mokxi in the 30 days to November 16, 2026.
The message

What the Arduino IDE prints

Output window
sketch.ino:5:5: error: redefinition of 'int counter'
sketch.ino.cpp.o: in function 'bump()': multiple definition of 'counter'
exit status 1

Declaring a thing is a mention. Defining it makes it exist, with its own memory. Each variable and each function body may exist once.

Mokxi's compiler, clang, says redefinition of 'counter' while it compiles, and duplicate symbol or multiple definition when it links.

Causes

The 3 usual causes with the fix

1setup() or loop() was pasted in twice

Two sketches pasted into one tab give two setup() and two loop(). Merge the bodies into one setup() and one loop().

Broken
void setup() {
  Serial.begin(9600);
}

void loop() {
}

void setup() {
  Serial.println("ready");
}
Fixed
void setup() {
  Serial.begin(9600);
  Serial.println("ready");
}

void loop() {
}

Mokxi’s build tab says: The sketch has two setup() functions

Try this fix in Mokxi

2A variable or a function is written twice

A second int counter = 0; at the top of the file is a second variable with the same name. Delete one, or give it its own name. A function with the same name and the same parameters is the same mistake.

Broken
int counter = 0;
int counter = 5;

void setup() {
  Serial.begin(9600);
  Serial.println(counter);
}

void loop() {
}
Fixed
int counter = 5;

void setup() {
  Serial.begin(9600);
  Serial.println(counter);
}

void loop() {
}

Mokxi’s build tab says: counter is defined twice. Keep one, or give the other a different name.

Try this fix in Mokxi

3A header defines a variable and two files include it

A header is pasted into every file that includes it. If it holds int counter = 0;, each file gets its own counter, and the linker finds two. Put extern in the header, and the one real definition in one file.

Broken: sketch.ino
#include "counter.h"

void setup() {
  Serial.begin(9600);
  bump();
  Serial.println(counter);
}

void loop() {
}
Broken: counter.h
int counter = 0;
void bump();
Broken: helper.cpp
#include <Arduino.h>
#include "counter.h"

void bump() {
  counter++;
}
Fixed: sketch.ino
#include "counter.h"

int counter = 0;

void setup() {
  Serial.begin(9600);
  bump();
  Serial.println(counter);
}

void loop() {
}
Fixed: counter.h
extern int counter;
void bump();
Fixed: helper.cpp
#include <Arduino.h>
#include "counter.h"

void bump() {
  counter++;
}

Mokxi’s build tab says: counter is defined in two files.

FAQ

Questions and answers

What does 'multiple definition of' mean in Arduino?

The linker found the same variable or function defined in two files. A header that holds a variable definition, included from two files, is the usual cause.

What is the difference between 'redefinition of' and 'multiple definition of'?

Redefinition is reported by the compiler when a name is defined twice in one file. Multiple definition is reported by the linker when it is defined once in each of two files.

Why is 'void setup()' redefined when I have one setup()?

Another tab or a pasted example has its own setup(). Every .ino tab joins the same sketch, so the sketch may hold only one.