arduino-cli
arduino-cli copied to clipboard
Prototype injected inside function definition when there is a line break after type
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
2947cfb71
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
- https://github.com/arduino/arduino-cli/issues/1944
- https://github.com/arduino/arduino-cli/issues/1253
- https://github.com/arduino/arduino-cli/issues/2696
- https://github.com/arduino/arduino-cli/issues/1800
- https://github.com/arduino/arduino-cli/issues/1591
- https://github.com/arduino/arduino-cli/issues/1618
- https://github.com/arduino/arduino-cli/issues/1785
- https://github.com/arduino/arduino-cli/issues/2161
- https://github.com/arduino/arduino-cli/issues/2697
Issue checklist
- [x] I searched for previous reports in the issue tracker
- [x] I verified the problem still occurs when using the nightly build
- [x] My report contains all necessary details