Fixing "variable or field declared void" in Arduino
Beginner
Or see every lesson.
- 505parts on the bench
- 45boards running now
- 1.00xreal time, on every board
variable or field 'setup' declared void means GCC read a line that starts with void as a variable, not as a function, and a variable cannot be of type void. Either the round brackets after the name are missing, as in void setup {, or something inside them is not a type the compiler knows, such as Servo without its #include, or a misspelled itn. Then GCC cannot read the brackets as a parameter list, and falls back to reading a variable with a starting value.
The name in quotes is the function on that line, so look there. Mokxi compiles with clang, which words the two cases differently: variable has incomplete type 'void' when the brackets are missing, and unknown type name when a type is unknown. The circuit above is the first blink. Open it, delete the () after void setup and press Run to see it.
"variable or field 'setup' declared void": the brackets are missing
A function’s name is followed by round brackets, even when it takes nothing: void setup(). Without them, void setup { reads as a variable called setup, of type void, with a list of starting values in braces. GCC 13 stopped with variable or field 'setup' declared void, then expected '}' before ';' token and expected declaration before '}' token for the lines inside. void loop { gave variable or field 'loop' declared void.
Clang said variable has incomplete type 'void' at the name, then unexpected ';' before '}' and expected ';' after top level declarator. Mokxi’s build tab explains the first: "This reads as a variable of type void. A function needs round brackets after its name: void blink() { ... }." Put the () back.
void setup {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}A parameter type the compiler does not know
This is the cause behind most of the forum posts. A function that takes a library’s type, void moveTo(Servo &s, int angle), in a sketch without #include <Servo.h>, gave GCC’s variable or field 'moveTo' declared void, then 'Servo' was not declared in this scope. GCC does not know Servo is a type, so it reads (Servo &s, int angle) as a starting value made from two variables, and the line as a void variable. The second error is the real one.
Clang names the cause right away: unknown type name 'Servo', and the build tab says to add #include <Servo.h> at the top. A misspelled type does the same: void blinkTimes(itn times) gave GCC’s variable or field 'blinkTimes' declared void and 'itn' was not declared in this scope; did you mean 'int'?, and clang’s unknown type name 'itn'. Add the include, or fix the spelling.
A struct below the first function, in the Arduino IDE
The Arduino IDE writes a prototype, a one-line declaration, for every function in the sketch and puts them near the top, ahead of the first function, so you can call a function above the place it is written. If a function takes a struct that the sketch defines further down, its prototype names the struct before the struct exists.
We checked it with g++ 13 on the sketch below, with the prototypes put ahead of setup() as the IDE puts them: variable or field 'show' declared void, then 'Reading' was not declared in this scope, at the prototype. Mokxi leaves out a prototype that names a type declared later, as plain C++ would have no prototype at all, so the same sketch built here and printed 512. For the IDE, move the struct to the top of the sketch, above setup(), or put it in a .h tab and include it.
void setup() {
Serial.begin(9600);
}
struct Reading {
int raw;
};
void show(Reading r) {
Serial.println(r.raw);
}
void loop() {
Reading r = {512};
show(r);
delay(1000);
}Open this sketch in the editorThe code alone: the editor places the board and the parts it can read from the sketch.
Read the error under it first
In every case above but the missing brackets, GCC’s first message is a side effect, and the second one names the cause: 'Servo' was not declared in this scope, 'itn' was not declared in this scope; did you mean 'int'?, 'Reading' was not declared in this scope. So when you see variable or field ... declared void, go to the next error, find the name it gives, and ask why the compiler does not know that name yet. The answer is nearly always one of three: a missing #include, a spelling, or a type that is defined further down the sketch.
If there is no second error, the line itself is the whole problem. Read it the way C++ reads it: a type, then a name, then either round brackets, for a function, or an = and a value, for a variable. A function line with no brackets, or a variable with void in front of it, shows up at once.
void used for a variable
void is only for functions that give nothing back, and for a few uses with pointers. void ledPin = 13; gave GCC’s variable or field 'ledPin' declared void and clang’s variable has incomplete type 'void'. Give the variable a real type: const int ledPin = 13;. The same goes for a local variable inside a function, which GCC reports with the same words.
Open this circuit in the editor. Arduino Uno: the first blink, wired as it runs at the top of this page. Change a part or a value and run it again.
Open the circuitQuestions
What does "variable or field 'setup' declared void" mean?
GCC read void setup as a variable rather than a function. Usually the round brackets are missing, void setup {, so put them back: void setup() {.
Why does a function with a Servo parameter give this error?
The compiler does not know Servo yet, so it cannot read the brackets as parameters. Add #include <Servo.h> at the top of the sketch. The second error, 'Servo' was not declared in this scope, names the cause.
Why does my sketch build in Mokxi but not in the Arduino IDE?
If a function takes a struct defined below the first function, the IDE’s prototype for it comes before the struct and fails. Mokxi leaves that prototype out. Move the struct to the top of the sketch.
What does clang say instead?
variable has incomplete type 'void' when the brackets are missing or a variable is void, and unknown type name 'Servo' when a parameter’s type is not known.
Keep going
Where the facts on this page come from
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.