Fixing "redefinition of 'void setup()'" and other redefinitions
Beginner
Or see every lesson.
- 455parts on the bench
- 43boards running now
- 1.00xreal time, on every board
redefinition of 'void setup()' means the sketch defines setup() twice. It almost always comes from pasting a second example under the first, or from a second tab that brings its own setup(). A sketch has exactly one setup() and one loop(): move the lines of the second setup() into the first, the lines of the second loop() into the first, and delete the empty copies. The same message with any other name, such as redefinition of 'int ledPin', means that name is defined twice.
Mokxi compiles with clang, which words it redefinition of 'setup' and adds a note, previous definition is here, on the first one. The circuit above is a button and an LED on an Uno: hold the button and the LED lights. Below, a second sketch that prints the time gets pasted under it, the build stops, and the two are merged into one that does both.
Two sketches pasted into one
The button sketch ends at line 18. Paste a second sketch under it, with its own setup() that starts the serial port and its own loop() that prints the seconds, and the build stopped with two errors: redefinition of 'setup' on line 20 and redefinition of 'loop' on line 24, each with a note at the first one, on lines 9 and 14. Mokxi’s build tab says "The sketch has two setup() functions, often from pasting a second example under the first. Keep one setup() and move the lines of the other into it."
Merging is mostly copying lines across. Both setups’ lines go into one setup(). The loops need one more thought, because a delay(1000) copied into the button’s loop would make the button wait up to a second. Time the printing with millis() instead, and both run together. The merged sketch below lit the LED while the button was held and printed "seconds: 1", "seconds: 2" and "seconds: 3" in its first three and a half seconds.
const int BUTTON_PIN = 2;
unsigned long lastPrint = 0;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP); // from the button sketch
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600); // from the second sketch
}
void loop() {
bool pressed = digitalRead(BUTTON_PIN) == LOW;
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
if (millis() - lastPrint >= 1000) { // the second sketch's loop, without delay()
lastPrint += 1000;
Serial.print("seconds: ");
Serial.println(millis() / 1000);
}
delay(5);
}A second tab with its own setup()
A sketch split into several .ino tabs is joined into one file before it is compiled: the main tab first, then the others in alphabetical order, in the Arduino IDE and in Mokxi. A second tab with a setup() of its own is two setups in one file. With a tab called extra.ino that starts the serial port, the error pointed at extra.ino line 1, and the note at sketch.ino line 1.
Give the second one a name of its own, such as void setupSerial(), and call it from the real setup().
A .cpp tab is different: it is compiled on its own, so the compiler never sees both. The linker does. GCC’s linker says multiple definition of 'setup', and Mokxi’s says duplicate symbol: setup(). The fix is the same.
The same variable declared twice
int ledPin = 13; and then int ledPin = 12; lower down is two variables with one name, and the second line stops the build: GCC says redefinition of 'int ledPin', clang redefinition of 'ledPin'. Delete one of them. Inside a function it is usually a second line that meant to change the value: int total = 0; and then int total = analogRead(A0); gave redefinition of 'total'. Leave the type off the second line, total = analogRead(A0);, and it changes the first variable instead of making another.
A name the Arduino core already uses
The core already has functions called delay, random, millis and map, and a global variable cannot have the same name. int delay = 500; stopped the build: clang says redefinition of 'delay' as different kind of symbol, with a note pointing at void delay(uint32_t ms); in the core, and GCC says 'int delay' redeclared as different kind of entity (older versions say symbol). Mokxi’s build tab offers to rename yours to myDelay everywhere the sketch uses it. int random = 4; gave the same error.
One name depends on the board. A global unsigned long time = 0; builds on an Uno, in the Arduino IDE and in Mokxi, because the Uno’s core never declares the C library’s time() function. The ESP32 and ESP8266 cores do, for every sketch, so on those boards the same line stops the build with redefinition of 'time' as different kind of symbol, as delay does. Call it lastTime or startTime, which says more anyway, and it builds on every board.
A header included twice
A tab blinker.h that declares class Blinker { ... }; and is included twice, directly or through another header, declares the class twice. Clang says redefinition of 'Blinker', with notes that the file was included multiple times and unguarded header; consider using #ifdef guards or #pragma once.
Put #pragma once on the first line of every header you write. With it, the same project built, both #include lines and all.
#pragma once
class Blinker {
public:
Blinker(int pin) : pin_(pin) {}
void tick() { digitalWrite(pin_, !digitalRead(pin_)); }
private:
int pin_;
};Questions
What does "redefinition of void setup()" mean?
The sketch has two setup() functions, usually because a second example was pasted under the first or a second tab has its own. Keep one setup() and move the other’s lines into it, and do the same for loop().
How do I combine two Arduino sketches into one?
Put the global variables and #include lines of both at the top, both setups’ lines in one setup(), and both loops’ lines in one loop(). Replace any delay() in the loops with millis() timing, so neither part waits for the other.
Is "redefinition of 'setup'" the same error?
Yes. It is clang’s wording; GCC, which the Arduino IDE uses, says "redefinition of 'void setup()'". Mokxi compiles with clang, so its build tab shows clang’s words.
Why can I not call a variable delay or random?
The Arduino core already has functions with those names, and one name cannot be both a function and a variable. Pick another name, such as myDelay or waitMs.
Keep going
Where the facts on this page come from
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.