Undefined reference to 'setup()', compiling the wrong main.cpp?

Hi everybody!
First of all, so thankful for this forum. It appears like a really supportive community.

Here’s my problem today: compiling my main.cpp for a fairly involved project, file structure below. I’ve tried swapping out my main.cpp for something simple like:
#include <Arduino.h>
void setup() {
Serial.begin(115200);
Serial.println(“Setup running”);
}
void loop() {
Serial.println(“Loop running”);
delay(1000);
}

But I persistently get these errors:
Scanning dependencies…
Dependency Graph
|-- AsyncTCP-esphome @ 2.1.4
|-- ESPAsyncWebServer-esphome @ 3.3.0
|-- ArduinoJson @ 7.3.1
|-- WebSockets @ 2.6.1
|-- SPIFFS @ 2.0.0
Building in debug mode
Linking .pio\build\esp32dev\firmware.elf
c:/users/rober/.platformio/packages/toolchain-xtensa-esp32/bin/…/lib/gcc/xtensa-esp32-elf/8.4.0/…/…/…/…/xtensa-esp32-elf/bin/ld.exe: .pio\build\esp32dev\libFrameworkArduino.a(main.cpp.o):(.literal._Z8loopTaskPv+0x8): undefined reference to setup()' c:/users/rober/.platformio/packages/toolchain-xtensa-esp32/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio\build\esp32dev\libFrameworkArduino.a(main.cpp.o):(.literal._Z8loopTaskPv+0xc): undefined reference to loop()’
c:/users/rober/.platformio/packages/toolchain-xtensa-esp32/bin/…/lib/gcc/xtensa-esp32-elf/8.4.0/…/…/…/…/xtensa-esp32-elf/bin/ld.exe: .pio\build\esp32dev\libFrameworkArduino.a(main.cpp.o): in function loopTask(void*)': C:/Users/rober/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp:42: undefined reference to setup()’
c:/users/rober/.platformio/packages/toolchain-xtensa-esp32/bin/…/lib/gcc/xtensa-esp32-elf/8.4.0/…/…/…/…/xtensa-esp32-elf/bin/ld.exe: C:/Users/rober/.platformio/packages/framework-arduinoespressif32/cores/esp32/main.cpp:48: undefined reference to `loop()’
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\esp32dev\firmware.elf] Error 1
================================================================================= [FAILED] Took 18.84 seconds =================================================================================

So I followed the link in the terminal and it takes me to:
C:\Users\rober.platformio\packages\framework-arduinoespressif32\cores\esp32\main.cpp

but it should be compiling the main.cpp here:
C:\Users\rober\OneDrive\Documents\PlatformIO\Projects\ES32A08-SNRv2\src\main.cpp

This is driving me mental. Can anyone possibly advise? Here’s my platform.ini in case it helps:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
esphome/AsyncTCP-esphome@^2.1.4
esphome/ESPAsyncWebServer-esphome@^3.3.0
bblanchon/ArduinoJson@^7.3.1
links2004/WebSockets@^2.6.1
build_src_filter =
+<src/main.cpp> ; Explicitly include src/main.cpp
+<src/web/.cpp> ; Include all .cpp files in src/web/
+<src/ui/
.cpp> ; Include all .cpp files in src/ui/ (if any)
+<controllers/.cpp> ; Include all .cpp files in controllers/
+<storage/
.cpp> ; Include all .cpp files in storage/
+<web/*.cpp> ; Include all .cpp files in web/ (if outside src/)
board_build.partitions = default.csv
build_type = debug
monitor_speed = 115200
build_flags =
-D CORE_DEBUG_LEVEL=0

Thanks in advance,
Roberto

Actually, I understand now that the main.cpp in the esp32 directory is actually trying to call the loop and the setup from my main.cpp. So, now I’m more perplexed than ever. Much sadness. Any help is always appreciated. How do I get the esp32/main.cpp to see the setup() and loop() in my main.cpp.

The build_src_filter paths are interpreted as relative to the src/ directory. So, if you have

project root
  - platformio.ini
  - src/
    - main.cpp

Then you just told PlatformIO to build absolutely nothing from your project.

See documentation

https://docs.platformio.org/en/latest/projectconf/sections/env/options/build/build_src_filter.html#build-src-filter

1 Like

This is most helpful information.

By playing around on my own, I was able to get the system to compile. I basically took all the cpp code and refactored it so that everything was a subdirectory of the src/ folder and it was a successful compile. I’ll keep your link handy though, I probably should go though in detail before carrying on.