From 21791a8237e1d33ca9cfcf1ed2f8db6fc86e4d62 Mon Sep 17 00:00:00 2001 From: David Montero Date: Sat, 23 May 2026 17:41:21 +0200 Subject: [PATCH] fix(espidf): drop textual #include "sketch.ino.cpp" in main wrapper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now that main/CMakeLists.txt globs every *.cpp/*.c into SRCS (commit 27c9a28 — needed so multi-file Arduino projects link), sketch.ino.cpp is compiled as its own translation unit. The template main.cpp's `#include "sketch.ino.cpp"` is left over from when SRCS only named main.cpp — back then we had to pull the user code into main.cpp's TU to get it compiled at all. With the glob in place, keeping the include re-defines setup() and loop() in main.cpp's TU AND in sketch.ino.cpp.obj, so the linker dies with "multiple definition of `setup'". Replace the include with a forward declaration and let the linker resolve setup()/loop() from sketch.ino.cpp.obj. Matches arduino-cli's per-file compile + auto-link model, and frees pure-C++ multi-file sketches to put helper TUs in their own .cpp files without textual-include hacks. Repro: robot-desktop-eyes after the glob fix (#13 rebuild) — compile got past resolve+compile and into link, where multi-def of setup() would have appeared on the next attempt. This commit closes the loop. --- .../services/esp-idf-template/main/main.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/backend/app/services/esp-idf-template/main/main.cpp b/backend/app/services/esp-idf-template/main/main.cpp index bd2a7623..bc382303 100644 --- a/backend/app/services/esp-idf-template/main/main.cpp +++ b/backend/app/services/esp-idf-template/main/main.cpp @@ -1,12 +1,22 @@ /** * Arduino-as-ESP-IDF-component wrapper. * - * The build system copies the user's .ino sketch as "sketch.ino.cpp" into - * this directory. initArduino() initializes the Arduino runtime (GPIO, - * Serial, WiFi, etc.) and then we call the user's setup()/loop(). + * The espidf_compiler writes the user's entry .ino as "sketch.ino.cpp" + * into this directory and drops every helper .h/.cpp alongside it. + * The CMakeLists.txt globs *.cpp/*.c into SRCS, so each TU compiles + * standalone — setup() and loop() are defined exactly once across the + * link (in sketch.ino.cpp) and we just call them from app_main() with + * a forward declaration. + * + * Note: do NOT switch this back to `#include "sketch.ino.cpp"` — the + * textual include would re-define setup()/loop() inside this TU as + * well, fighting the standalone sketch.ino.cpp.obj for the symbol + * and tripping the linker with "multiple definition of `setup'". */ #include "Arduino.h" -#include "sketch.ino.cpp" + +void setup(); +void loop(); extern "C" void app_main(void) { initArduino();