Arduino compile errors, what they mean and how to fix them
- 192parts on the bench
- 25boards running now
- 1.00xreal time, on every board
A compile error means the compiler could not turn your sketch into a program, so nothing was uploaded and nothing ran. The Arduino IDE ends the output with "exit status 1" or "Compilation error", which only says that it failed. The useful part is the first line that says error, with a file name, a line number and a column in front of it.
This page goes through the errors beginners hit most, with what each one means and the fix. The Arduino IDE compiles with GCC and Mokxi compiles with clang, so the same mistake is worded differently in each; both are given, taken from real builds of the broken sketches. Open the blink circuit above in the editor, make each mistake on purpose, and press Run to see the error where it happens.
Read the first error, and look one line up
Fix errors from the top. One missing brace can produce a dozen errors below it, and they all disappear with the first fix. A line such as `sketch.ino:6:3: error:` means line 6, column 3.
The line named is where the compiler noticed, which is not always where you made the mistake. A missing semicolon or quote at the end of line 5 is often reported on line 6, and a missing closing brace is reported at the very end of the file. If a line looks fine, read the one above it.
"was not declared in this scope"
GCC says `'digitalWrit' was not declared in this scope`; clang says `use of undeclared identifier 'digitalWrit'`. Both mean the name is not one the compiler knows at that point. Newer versions of both add a suggestion, such as `did you mean 'digitalWrite'?`, which is usually right.
The usual causes, in order: a typo; the wrong case, because C++ is case sensitive and `digitalwrite` is not `digitalWrite`; a variable declared inside a pair of braces and used outside them, since a variable only exists inside the block that declares it; and a library object used without its `#include` line. Declaring the variable before the block starts fixes the third.
void setup() {
Serial.begin(9600);
}
void loop() {
int count = 0; // declared here, so it is visible below the loop
for (int i = 0; i < 3; i++) {
count = count + i;
}
Serial.println(count);
delay(1000);
}"does not name a type"
GCC: `'Servo' does not name a type`. Clang: `unknown type name 'Servo'`. The compiler met a type it has never heard of, almost always because the `#include <Servo.h>` line is missing or misspelled. Header names are case sensitive too: `servo.h` is not `Servo.h`.
"expected ';' before"
GCC: `expected ';' before 'delay'`, pointing at the next statement. Clang: `expected ';' after expression`, pointing just after the one that is missing it, with a marker showing where the semicolon goes. Every statement in C++ ends with a semicolon; lines that open a block, like `void loop() {` or `if (x > 5) {`, do not.
A related one is `missing terminating " character`, which means a string lost its closing quote. It usually brings more errors after it, which all go away once the quote is back.
Braces: "expected '}' at end of input"
GCC: `expected '}' at end of input`. Clang: `expected '}'`, with a note pointing at the opening brace it could not match. Somewhere a `{` has no `}`. Indenting each block one level makes the missing one easy to see: the Arduino IDE’s Auto Format (Ctrl+T) does it for you.
A missing brace also causes `a function-definition is not allowed here before '{' token` (clang: `function definition is not allowed here`). The compiler thinks you are still inside `loop()` when the next function starts. Close the function above it.
Code outside a function
A statement such as `digitalWrite(13, HIGH);` sitting between functions, not inside one, gets `expected constructor, destructor, or type conversion before '(' token` from GCC and `a type specifier is required for all declarations` from clang. Neither says it plainly: outside a function you can declare things (variables, constants, functions) but not do things. Move the line into `setup()` or `loop()`.
Two setups, or no loop
`redefinition of 'void setup()'` (clang: `redefinition of 'setup'`) comes from pasting two sketches into one file. A sketch has one `setup()` and one `loop()`: merge the two bodies into one of each.
Leaving one out entirely is a linker error rather than a compiler error. GCC’s linker reports `undefined reference to 'loop'`, and Mokxi’s reports `undefined symbol: loop()`. Every sketch needs both functions, even if one is empty.
A missing library is reported as `NoSuchLib.h: No such file or directory` by GCC and `'NoSuchLib.h' file not found` by clang. In the Arduino IDE, install it from the Library Manager. Mokxi cannot install libraries, but it carries its own versions of the common ones under their usual names, and for the rest the build tab names the header to use instead.
Function order and warnings work as they do in the Arduino IDE
Plain C++ wants a function declared before it is used, but tutorials often put `loop()` first and the functions it calls below it. The Arduino IDE makes that work by writing a prototype (a one-line declaration) for every function in the sketch before compiling it, and Mokxi writes the same prototypes, so the sketch below builds in both as it is. Mokxi also joins several `.ino` tabs into one file the way the IDE does, main tab first, so a function in one tab can be called from another.
A few functions get no prototype, in the IDE or in Mokxi, and have to come before the code that calls them: template functions, functions inside a class or a namespace, and, in Mokxi, a function whose parameter types are declared further down the sketch. If a call is still an undeclared identifier, move the function above it or write the prototype yourself at the top. A prototype you write is always fine.
Warnings are shown, not fatal. Mokxi compiles your sketch with the compiler’s extra warnings on and lists them in the build tab, but a warning never stops the build, just as in the Arduino IDE, which hides warnings by default. An unused variable, an unused function parameter and comparing an `int` with the result of `sizeof` are warnings you can leave for later. `if (x = HIGH)` is worth fixing now: a single `=` assigns, so the condition is always true, and you meant `==`.
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
blinkTwice();
delay(1000);
}
void blinkTwice() {
for (int i = 0; i < 2; i++) {
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
}
}Questions
What does "exit status 1" mean in Arduino?
Only that the compiler stopped with an error. Scroll up to the first line containing "error:"; that is the message to fix.
Why does the error point at the wrong line?
The compiler reports where it noticed the problem. A missing semicolon or quote shows up on the next line, and a missing brace at the end of the file.
Why does my sketch compile in the Arduino IDE but not in Mokxi?
Usually a library Mokxi does not carry, or a part of the Arduino core that board’s runtime leaves out; the build tab says which. Function order and warnings are not the cause: Mokxi writes the same function prototypes the IDE does, and a warning is shown without stopping the build.
Does Mokxi use the same compiler as the Arduino IDE?
No. The Arduino IDE uses GCC (avr-gcc for the Uno) and Mokxi uses clang. Both compile the same C++, so the sketch is the same; the error messages are worded differently.
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.