Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork436
Open
Description
Describe the problem
Unable to compile a function template after a struct definition. The code passed compilation with multiple different versions of the compilers onCompiler Explorer, but fails in Arduino IDE.
To reproduce
Setup environment
$ arduino-cli versionarduino-cli Version: git-snapshot Commit: eb4e2ca77 Date: 2025-08-14T05:11:39Z$ mkdir "/tmp/FooSketch"$ echo 'struct foo_t{};template<typename T>void bar(foo_t Parameter) {}void setup() {}void loop() {}' > "/tmp/FooSketch/FooSketch.ino"Demo
$ arduino-cli compile --fqbn arduino:avr:uno "/tmp/FooSketch"C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:2:30: error: variable or field 'bar' declared void struct foo_t{}; ^C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:2:30: error: 'foo_t' was not declared in this scopeC:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:2:30: note: suggested alternative: 'fpos_t' struct foo_t{}; ^ fpos_tUsed platform Version Patharduino:avr 1.8.6 C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6Error during build: exit status 1🐛 There was a spurious compilation failure of valid code.
By looking at the C++ code generated by the Arduino sketch preprocessor, we can see the cause of the error:
$ arduino-cli compile --fqbn arduino:avr:uno --preprocess "/tmp/FooSketch"#include <Arduino.h>#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"#line 2 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"template<typename T>void bar(foo_t Parameter);#line 5 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"void setup();#line 6 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"void loop();#line 2 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"struct foo_t{};template<typename T>void bar(foo_t Parameter) {}void setup() {}void loop() {}🐛 The spurious compilation failure was caused by Arduino CLI placing the prototype for thebar function before the declaration of thefoo_t type.
Expected behavior
Successful compilation
Arduino CLI version
Operating system
Windows
Operating system version
11
Additional context
Related
- Compilation fails due to function reordering #500
- #line directives inserted into raw string literal by sketch preprocessor #1191
- ) in comment breaks function prototype generation #1253
- Generated prototype incorrectly prefixed with
extern "C"when comment contains//#1591 - Generated prototype incorrectly prefixed with
extern "C"when usingextern "C" { ... }to mix C functions in an.inofile. #1618 - Backslash incorrectly inserted into generated prototype for multiline template function #1785
- Prototype incorrectly generated for struct member function under certain conditions #2161
- https://forum.arduino.cc/t/is-the-location-of-a-global-function-important/996378/18
- https://forum.arduino.cc/t/ide-mistakes-javascript-function-for-type/953022
- https://forum.arduino.cc/t/c-super-quotes-compiler-bug/990071
- https://forum.arduino.cc/t/over-length-line-in-editor-forces-forward-declaration/955571
- https://forum.arduino.cc/t/solved-multiple-ide-tabs-issue/988086
- https://forum.arduino.cc/t/declaration-of-functions/687199/11
Workaround
Manually add a function prototype at the appropriate location:
structStruct{};template<typename T>voidFunction(Struct Parameter);// Manually added function prototype to work around prototype generator bug.template<typename T>voidFunction(Struct Parameter) {}voidsetup() {}voidloop() {}
Issue checklist
- I searched for previous reports inthe issue tracker
- I verified the problem still occurs when using the latestnightly build
- My report contains all necessary details