PlatformIO Linker Error: Undefined Reference to 'setup' or 'loop'

Diagnostic signature

undefined reference to 'setup'

The linker failed to find the setup() or loop() function definitions required by the Arduino framework's core main() entry point.

What it means

When compiling an Arduino framework project in PlatformIO, the underlying C++ `main()` function is provided by the Arduino core library, not the user's code. This internal `main()` function explicitly calls a user-defined `setup()` function once for hardware initialization, followed by continuous calls to a `loop()` function for the main application logic. When the GCC linker (ld) links the compiled core library with the user's application code, it attempts to resolve these function calls. If it cannot find the symbols for `setup` or `loop` in the user's compiled object files, it emits an "undefined reference" error, indicating that the framework's entry points are missing.

Why it happens

This diagnostic is most commonly triggered when a project is configured with `framework = arduino` in the `platformio.ini` environment, but the user has written a standard C/C++ application utilizing an `int main()` function instead of the required Arduino entry points. It also occurs if a developer forgets to define `setup()` or `loop()` entirely, misspells the function names (e.g., `Setup()` instead of `setup()`), omits the `void` return type, or places the definitions inside a namespace or class without exposing them to the global scope.

Minimal reproduction

#include <Arduino.h>

void loop() {
  // The loop is defined, but setup() is missing.
}

How to fix it

Ensure both void setup() and void loop() are correctly defined when using the Arduino framework, or remove the framework dependency if writing a bare-metal C++ application with a standard main().

```C++
#include <Arduino.h>

void setup() {
  // Hardware initialization code here
}

void loop() {
  // Main program execution here
}

```

Step 1

Step 2

Step 3

Step 4

Upstream references

ArduinoCore-avr: main.cpp implementation

Arduino Language Reference: setup()