Learn about compilation errors in your sketch and how to resolve them.
Quick checks
-
Make sure your error occurs during compilation by clicking
Verify instead of
Upload. This will compile the sketch without attempting to upload it. If your error only occurs when uploading, see Errors when uploading a sketch.
-
A successful compilation will always finish with this message (the storage space and memory values might differ depending on the board used):
Sketch uses 11604 bytes (4%) of program storage space. Maximum is 262144 bytes. Global variables use 2980 bytes (9%) of dynamic memory, leaving 29788 bytes for local variables. Maximum is 32768 bytes.
-
A sketch always needs to include a
setup()
andloop()
function, even if they’re not being used. You can use File > Examples > 1.Basics > BareMinimum as a template. -
Libraries added with
#include
need to be installed. Try searching for the library name in the Library Manager (Tools > Manage Libraries…).
Interpreting the error message
Arduino IDE displays compilation messages differently depending on the version:
-
Check the IDE error message.
- In IDE 1.x, it is displayed above the console window.
- In IDE 2, it is displayed in a bottom-right pop-up. Sometimes, preceding lines can be more informative — check the console output.
-
Check the console output.
- Compilation errors will often reference the row and column where the error was triggered, i.g.
Blink:29:1
(line 29, 1 column). - You may have to scroll or expand the window width to see the entire message.
- Compilation errors will often reference the row and column where the error was triggered, i.g.
-
Look for highlights in the editor:
- In IDE 1.x, the line where the error occurred is highlighted in red.
- In IDE 2, the character where the error occurred is underlined in red.
- The line causing the error may be before the line where it was triggered. In the example above, line 28 is missing a semicolon (
;
), but the error will be triggered by the unexpected closing bracket (}
) in line 29.
Error output in IDE 1.x. Error output in IDE 2.
Troubleshoot a specific error
undefined reference to ‘setup’ or ‘loop’
collect2: error: ld returned 1 exit status
Occurs when the setup()
or loop()
functions are missing. Your sketch must include these functions, even if they’re not being used. You can use File > Examples > 1.Basics > BareMinimum as a template. Note that function names are case-sensitive and that the compiler will handle something like Setup()
(with a capital S) as a completely different function.
fatal error: <library>: No such file or directory
For example:
sketch_may11a:22:10: fatal error: KeyboardController.h: No such file or directory
#include <KeyboardController.h>
^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
exit status 1
KeyboardController.h: No such file or directory
- Check that the library name is spelled correctly and included with the proper format, e.g.
#include <SPI.h>
. - Make sure to import the
.h
file.
variable was not declared in this scope
Occurs when a variable is accessed before being declared with the proper syntax, e.g. int i = 5
, or if the variable is declared outside the current scope.
In this exampled, the variable i
is declared in the setup()
function, and then accessed in the loop()
function.
void setup() {
// put your setup code here, to run once:
int i = 5;
}
void loop() {
// put your main code here, to run repeatedly:
i = i + 1;
}
Because the setup()
function’s scope is not accessible from loop()
, the compiler will return this error:
sketch_may16a:3:3: error: 'i' was not declared in this scope
i = 5;
^
exit status 1
'i' was not declared in this scope
Instead of declaring i
in setup()
, it can be declared as a global variable:
int i = 5;
void setup() {
// put your setup code here, to run once
}
void loop() {
// put your main code here, to run repeatedly:
i = i + 1;
}
expected unqualified-id before numeric constant
Will occur if a comma (,
) is used as a decimal separator instead of period (.
).
This float is incorrectly assigned 1,1
:
float f = 1,1;
It will trigger this error:
sketch_may16a:3:15: error: expected unqualified-id before numeric constant
float f = 1,1;
^
exit status 1
Instead, use a decimal point (.
):
float f = 1.1;
expected ‘,’ or ‘;’ before ‘:’ token
sketch_may16a:3:19: error: expected ',' or ';' before ':' token
int i = 1:
^
exit status 1
This error occurs because a line has been ending with a colon (:
)
instead of a semicolon. (;
). The line should look like this:
int i = 1;
‘expected declaration before ‘}’ token’, or ‘expected ‘}’ at end of input’
These errors can occur when brackets ({
and }
are incorrectly used.
- Make sure that brackets are opened and closed in the expected order.
- Use Tools > Auto Format to make your code more readable.
- If you click on a bracket, the associated opening or closing bracket will be highlighted.
Still need help?
-
Arduino sketches are written in the Arduino language, which is based on standard C++ language. Most likely you will find a wealth of resources by searching
C++ <error message>
in your search engine. -
For help with functions specific to Arduino, see the Arduino functions reference.
-
Visit the Programming Questions category in the Arduino forum. Start by reading the pinned threads which will contain useful information on how to best post a question.