Fix “undefined reference to `setup'” (or `loop')
The linker found no setup() or loop(): a capital letter (Setup), an empty or unsaved sketch, or code in the wrong file. Name them exactly setup() and loop().
Community draft — report a mistake
What you see
C:\Users\Asha\AppData\Local\Temp\ccprt5UM.ltrans0.ltrans.o: In function `main':
cores\arduino/main.cpp:43: undefined reference to `setup'
collect2.exe: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1C:\Users\Asha\AppData\Local\Temp\ccprt5UM.ltrans0.ltrans.o: In function `main': cores\arduino/main.cpp:43: undefined reference to `setup' collect2.exe: error: ld returned 1 exit status exit status 1 Compilation error: exit status 1
“Undefined reference” is a linker error: every file compiled, but something that was called is defined nowhere. For setup and loop, that almost always means a spelling or case slip.
void setup() { // not Setup(), not setUp()
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, !digitalRead(13));
delay(500);
}Quick fix — try in order
- 1
Check the spelling and case
It must be exactly
void setup()andvoid loop(), all lowercase.Setup()orvoid loop ()inside a comment doesn’t count. - 2
Save the sketch
An unsaved new sketch or an .ino that is empty on disk compiles as nothing. Save (Ctrl+S) and compile again.
- 3
Put them in the main .ino
setup() and loop() must be in the sketch, not only in a .h file that is never included.
- 4
Not inside another function
A missing
}can put loop() inside setup(). Fix braces first — the compiler error for that comes before this one.
Why it happens
Why does main.cpp appear?
The Arduino core has its own main() that calls your setup() once and loop() forever. If your sketch doesn’t provide them, the linker can’t complete that call.
undefined reference to a library function
undefined reference to `Adafruit_SSD1306::begin(…)' means the header was found but the library’s .cpp was not compiled — a broken or partial library install. Reinstall it from the Library Manager.
undefined reference to `vtable for …'
A class declares a virtual function but never defines it. Usually an incomplete copy of a library.
Board-specific notes
ESP32: The ESP32 core’s main runs inside a FreeRTOS task (loopTask) but the rule is the same: one setup(), one loop().
Related: The opposite — two copies — is multiple definition of.
Did this fix it?
Thanks — that helps us keep this page right.