-
-
Notifications
You must be signed in to change notification settings - Fork 414
Open
Labels
topic: build-processRelated to the sketch build processRelated to the sketch build processtopic: codeRelated to content of the project itselfRelated to content of the project itselftype: imperfectionPerceived defect in any part of projectPerceived defect in any part of project
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 on Compiler Explorer, but fails in Arduino IDE.
To reproduce
Setup environment
$ arduino-cli version
arduino-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 scope
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:2:30: note: suggested alternative: 'fpos_t'
struct foo_t{};
^
fpos_t
Used platform Version Path
arduino:avr 1.8.6 C:\Users\per\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
Error 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 the bar
function before the declaration of the foo_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.ino
file. #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:
struct Struct{};
template<typename T>
void Function(Struct Parameter); // Manually added function prototype to work around prototype generator bug.
template<typename T>
void Function(Struct Parameter) {}
void setup() {}
void loop() {}
Issue checklist
- I searched for previous reports in the issue tracker
- I verified the problem still occurs when using the latest nightly build
- My report contains all necessary details
Metadata
Metadata
Assignees
Labels
topic: build-processRelated to the sketch build processRelated to the sketch build processtopic: codeRelated to content of the project itselfRelated to content of the project itselftype: imperfectionPerceived defect in any part of projectPerceived defect in any part of project