arduino-cli
arduino-cli copied to clipboard
The type declared after any function is not visible as the parameter type for any other function below.
The type declared after any function is not visible as the parameter type for any other function below. Although it is suitable for other purposes. For example:
struct tword{
byte low;
byte high;
};
void none(){}
bool test(tword par){return true;}
void setup() {
word w;
tword* var = (tword*)(&w);
}
void loop(){}
It's ok.
void none(){}
struct tword{
byte low;
byte high;
};
//bool test(tword par){return true;}
void setup() {
word w;
tword* var = (tword*)(&w);
}
void loop(){}
Too it's ok
void none(){}
struct tword{
byte low;
byte high;
};
bool test(tword par){return true;}
void setup() {
word w;
tword* var = (tword*)(&w);
}
void loop(){}
Compilation fails with the error:
'tword' was not declared in this scope
Additional context
Additional reports
- https://github.com/arduino/arduino-cli/issues/1822
- https://github.com/arduino/arduino-cli/issues/2525
Hi @allnick. Thanks for your report.
During sketch preprocessing, the Arduino build system (which is implemented here in the Arduino CLI code base) automatically generates function prototypes in .ino files for any functions that don't already have a prototype. This turns out to be surprisingly difficult. Although the prototype generation system works fine for most sketches, under certain circumstances, it inserts the prototype at the wrong position in the code. That is the cause of the error you encountered.
You can see how the code looks after preprocessing here:
$ arduino-cli compile --fqbn arduino:avr:uno --preprocess Foo
#include <Arduino.h>
#line 1 "C:\\Foo\\Foo.ino"
#line 1 "C:\\Foo\\Foo.ino"
void none();
#line 8 "C:\\Foo\\Foo.ino"
bool test(tword par);
#line 10 "C:\\Foo\\Foo.ino"
void setup();
#line 15 "C:\\Foo\\Foo.ino"
void loop();
#line 1 "C:\\Foo\\Foo.ino"
void none(){}
struct tword{
byte low;
byte high;
};
bool test(tword par){return true;}
void setup() {
word w;
tword* var = (tword*)(&w);
}
void loop(){}
Note that the bool test(tword par); prototype was inserted above the declaration of the tword type.
The workaround is to simply manually add your own prototype in the correct location:
void none(){}
struct tword{
byte low;
byte high;
};
bool test(tword par); // function prototype
bool test(tword par){return true;}
void setup() {
word w;
tword* var = (tword*)(&w);
}
void loop(){}
Respected per1234. Thanks for your explanation. But I just moved the type declaration above all the functions. But your remark, I necessarily take into account.