How to fix Arduino / PlatformIO undefined reference to `loop()'

Problem:

While trying to compile your Arduino or PlatformIO project, you see an error message such as

/home/uli/.platformio/packages/[email protected]+2021r2-patch5/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: .pio/build/esp32dev/libFrameworkArduino.a(main.cpp.o):(.literal._Z8loopTaskPv+0x8): undefined reference to `loop()'

Solution

You have not declared a loop() function in your source code. Open main.cpp or your .ino source code file and start with the following (empty) loop() function which does nothing:

void loop() {
    // Nothing to do here since HTTPServer
    // is running in a separate thread
    delay(1000);
}

After you’ve added any void loop() { /* ... */} function to your sourcecode try to build/upload again and the error message should have disappeared.

If you want, you can also add code such as to print a message to the serial port every time the loop is run:

void loop() {
    // Nothing to do here since HTTPServer
    // is running in a separate thread
    Serial.println("Hello world!");
    delay(1000);
}