2 Easy fixes to Arduino error: “.h: No such file or directory”
Are you trying to run an Arduino sketch, but keep coming across a “No such file or directory” error? This is a pretty common error! Keep watching to learn more about 2 easy fixes for this error.
No such file error!
Error messages can be such a pain, but they’re supposed to tell us something about the error we made. Let’s take a look at the one below. If you look at the bottom portion of the Arduino IDE where the error message shows up, there’s this handy little button that says “copy error messages”.
If you click on it, it copies the error message inside the little window to the clipboard on the computer.
What you can do now is paste it into Google, for example, and do a search which can help you find out more about the error. Or you could paste it into a forum and say, “Hey, I’ve got this error message, please help me.”
For this situation, however, we’ve copied it and we’re going to paste it into a text editor so we can take a closer look at what the error message is actually saying.
Decoding the error
The first sentence just says which Arduino version is in use, which operating system is running, and which board is selected.
The second sentence actually starts getting into the error a little bit. The first thing it gives us is the name of the program. So, in this case, the name of the program was “Knob”.
If you look at the Arduino IDE, the the numbers on the left are called line numbers of the program and they’re a reference to help us find where different lines of code are. The “10” after “Knob” is the line number (in the Arduino IDE) that the error was detected on.
The “19” is a reference to how long that line of code is, so how many spaces or characters long is it.
Then it tells us the actual error. It says “servo.h: No such file or directory”. Why are we getting this error?
The error of our ways
Well, let’s take a look at line 10 and see what it says. It says “#include <servo.h>”
When we verify this code, what this line does is tell the Arduino IDE compiler “Hey, for this program to work, you need to go get this file servo.h”.
Let’s say you had a label making machine and you were trying to run the label maker and print some labels. To make it work, you have to put in a roll of labels. If you don’t have that roll of labels, then your label maker isn’t gonna work. So our program’s like the label maker and the file (servo) is the roll of labels.
So the error message is like “Hey, programmer, you said I needed this other file… but I looked for it and it’s not there. What gives? Let’s get to the bottom of this error message and go over two different scenarios.
Scenario 1 – fat fingers
This sketch is one that you’ve written. You’re actually the one who wrote the “#include” line. The first thing you should check is your spelling and capitalization. Maybe you spelled the name of the library incorrectly, or in this example, maybe you didn’t capitalize the right letters.
So “servo.h” should actually be a capital “S”, or written out, “Servo.h”. Now in this example, you’ll notice that the word servo changes color when correctly capitalized, and that’s because the library name “Servo” is recognized as a “key word” in the Arduino IDE. Keep in mind that might not be the case for all the libraries that you’re using.
Therefore you can’t use whether or not it color changed as an absolute indicator, but it can be helpful. It is amazing how long you can stare at a line of code and miss something like a spelling or a capitalization error.
Scenario 2 – missing files
In this scenario you’ve either downloaded or copied and pasted some code from the internet and you’re trying to run it for the first time. Now you start getting this error message. Let’s just assume that the original author of the program spelled the name of the library correctly.
We’ll go ahead and skip the troubleshooting associated with scenario 1. The next step would be to verify that we actually have the file this program is calling for. We must also ensure the file is in the correct place.
An easy way to check to see if you have that file is to be in the Arduino IDE and go to Sketch > Include Library, and then look for the name of that library.
Whatever library the #include statement was calling for, you want to look through this big long list for that library. If you don’t see its exact name in this list, this means you do not have that library installed appropriately (they’re all in alphabetical order which helps). If you don’t see it here, you’ll have to add the library.
The easiest way to add a library is to go to Sketch > Include Library > Manage Libraries. It will open up a dialogue box and you can search for a library. There’s so many libraries, you’re definitely going to want to filter.
Make sure you type the exact word that matches the #include line. Once you find the missing library click install. It will let you know that it’s installing the library and updating the list of libraries.
Next we can double-check it has successfully installed. Go to Sketch > Include Library and the installed library should now appear on the drop down list. Now, when it complies, you should no longer get the error.
Not all libraries are in this convenient pop-up window when you go to manage libraries. There’s tons of different ways to find Arduino libraries on the web. Often, if you’re downloading or copying a program from the internet, just go to the page where you got that program and see what library they’re referencing. Maybe they have a link to GitHub, for example, which is a place where people keep a lot of code libraries.
Usually the library is included in a .zip file package. Once you’ve downloaded the .zip file, you can go to the Arduino IDE and go to Sketch > Include Library > Add .ZIP library… You just have to navigate to where the file was downloaded. It will tell you “Library added to your libraries” just above the dark area where the original error had appeared.
Now when we go to Sketch > Include Library, the new library will appear in the drop-down list. Viola! You now know 2 ways to add a new library.
So we’ve discussed two possible scenarios that could cause the “No such file or directory” error to appear after you compile your sketch.
First, if you wrote the sketch, just double-check your spelling and capitalization.
Second, if you have copied code from someone else, make sure you have the correct libraries installed.
We hope you have a great one and we’ll see you next time! Bye!