Preview build — page text is a draft awaiting human review. Code badges come from real compiles. The flasher is real but not yet hardware-tested; the Playground is still a preview.
Linker error · C++

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().

All boardsUpdated 3 Nov 2026

Community draft — report a mistake

What you see

OutputOutput
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 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. 1

    Check the spelling and case

    It must be exactly void setup() and void loop(), all lowercase. Setup() or void loop () inside a comment doesn’t count.

  2. 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. 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. 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.

↑↓ to move · Enter to open