Description
Describe the problem
In order to make it easier for beginners to get started with writing Arduino sketches, and for the convenience of all users, Arduino CLI automatically generates and adds prototypes for functions defined in a .ino
file of a sketch.
Whitespace does not have syntactic significance to the C++ compiler, so users are free to format their code according to their preferences. This includes adding line breaks between any components of a function definition.
🐛 Arduino CLI chooses an incorrect point to inject the generated function prototype under the following conditions:
- The sketch contains a manual function prototype.
- The first function definition has a line break after the type.
This causes compilation to fail with a cryptic error.
To reproduce
Setup environment
$ arduino-cli version
arduino-cli Version: git-snapshot Commit: 2947cfb71 Date: 2025-06-10T23:45:14Z
$ mkdir -p "/tmp/FooSketch"
$ printf '
void setup();
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:5:11: error: two or more data types in declaration of 'loop'
void loop() {}
^
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino: In function 'int setup()':
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:4:1: error: ambiguating new declaration of 'int setup()'
setup() {}
^~~~~
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:2:6: note: old declaration 'void setup()'
void setup();
[...]
🐛 The compilation failed even though the sketch is valid C++.
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"
void setup();
void
#line 5 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
void loop();
#line 4 "C:\\Users\\per\\AppData\\Local\\Temp\\FooSketch\\FooSketch.ino"
setup() {}
void loop() {}
🐛 The compilation failure was caused by Arduino CLI injecting the function prototype for void loop()
inside the definition of the void setup()
function.
Expected behavior
Generated function prototypes are not injected inside function definitions.
Arduino CLI version
Operating system
N/A, Windows
Operating system version
11
Additional context
Originally reported at https://forum.arduino.cc/t/function-prototype-with-newline-compiler-error/1388103
Workaround
Refrain from adding a line break after the type in function definitions.
Related
- Emit a warning if a prototype is not generated due to known limitations #1944
- ) in comment breaks function prototype generation #1253
- Generated function prototype injected before declaration of custom parameter type #2696
- No generated prototype for function with multi-line parameters in unsaved sketch #1800
- 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
- Prototype not generated for function with inline attribute #2697
Issue checklist
- I searched for previous reports in the issue tracker
- I verified the problem still occurs when using the nightly build
- My report contains all necessary details