Learn about compilation errors in your sketch and how to resolve them.
In this article:
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.
-
Make sure you have the right board selected in the board selector or the Tools > Board menu. See Select board and port in Arduino IDE for more information.
-
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. Learn how to add libraries to Arduino IDE.
How to interpret an error message
To get more information about the error, we need to check the console output. See the example below:
-
If a pop-up notification is still showing, close it by clicking the x in the top-right corner.
-
Find the console output panel.
-
Read the first few lines. The following generic messages may appear near the end of the output:
collect2: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1
These generic error messages indicate that the compilation process failed but don’t provide specific details about the error, so it’s important to read the lines before them.
-
Look for lines starting with file paths. At this point, you may need to resize the window or scroll to the right to reveal the full lines. These files will often be pointing at
main.cpp
function, or a temporary copy of your sketch. -
In this example, the message
…main.ccp:43: undefined reference to `setup’
is the most informative:In this case, the error was caused by a missing
setup()
function.
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.