Fix "fatal error: xxx.h: No such file or directory" in the Arduino IDE
fatal error: Adafruit_Sensor.h: No such file or directory compilation terminated. exit status 1 Error compiling for board Arduino Uno.
- sketch_nov28a.ino:1:10: fatal error: DHT.h: No such file or directory 1 | #include <DHT.h> | ^~~~~~~ compilation terminated.
- Compilation error: Adafruit_Sensor.h: No such file or directory
- fatal error: WiFi.h: No such file or directory
- fatal error: ESP8266WiFi.h: No such file or directory
- exit status 1 Error compiling for board ESP32 Dev Module.
The compiler cannot find the header file included with #include: the library that provides it is not installed, or the selected board is wrong. Install the library (and its dependencies) from the Library Manager, check Tools > Board, then compile again.
What this error means
Before compiling, the C++ preprocessor replaces each #include <file.h> with the content of that file. It looks for it in the sketch folder, in the installed libraries (libraries folder of the sketchbook) and in those shipped with the core of the selected board (AVR, ESP32, ESP8266…). If it finds it nowhere, avr-gcc or xtensa-gcc stops at once with fatal error and compilation terminated. The IDE then sums up with exit status 1 and Error compiling for board … (IDE 1.8) or Compilation error: … (IDE 2). The file name tells you which library or core is missing.
Common causes
- Library not installed: the sketch comes from a tutorial using
DHT.h,Adafruit_SSD1306.horLiquidCrystal_I2C.h, never installed on your machine. - Missing dependency: the library is there, but it includes another missing library, typically
Adafruit_Sensor.h(the "Adafruit Unified Sensor" package) for DHT and BME280 sensors. - Wrong board selected:
WiFi.hfor ESP32 orESP8266WiFi.honly exist if the matching core is installed and selected; with "Arduino Uno", they cannot be found. - Library in the wrong place: ZIP extracted into an extra subfolder (
Lib-master/Lib-master/), copied outside thelibrariesfolder, or sketchbook moved. - Wrong name or case:
#include <dht.h>instead ofDHT.h(macOS and Linux are case-sensitive), or a local file included with<>instead of quotes.
Step-by-step fix
-
Spot the missing file
Read the name right after
fatal error:: that is what you need to provide. If the error comes from a library file (path insidelibraries/), a dependency of that library is missing. -
Install the library from the manager
Open Sketch > Include Library > Manage Libraries (Ctrl+Shift+I, or the Library icon on the left in IDE 2), search for the file or component name and install it. When the IDE offers to install dependencies, choose "Install all".
-
Or from the command line with arduino-cli
The names to install are the Library Manager names, not the
.hfile names.arduino-cli lib search "DHT sensor" arduino-cli lib install "DHT sensor library" "Adafruit Unified Sensor" arduino-cli lib list -
Check the board and install its core
In Tools > Board, pick the actual board. For
WiFi.hon ESP32, add the Espressif core URL in File > Preferences > Additional boards manager URLs, then install "esp32 by Espressif Systems" from the Boards Manager.arduino-cli core update-index --additional-urls https://espressif.github.io/arduino-esp32/package_esp32_index.json arduino-cli core install esp32:esp32 --additional-urls https://espressif.github.io/arduino-esp32/package_esp32_index.json arduino-cli compile --fqbn esp32:esp32:esp32 MySketch -
ZIP library: install it in the right place
Use Sketch > Include Library > Add .ZIP Library. When copying by hand, the library folder must directly contain its
.hfiles (or asrc/folder) and sit in the sketchbooklibrariesfolder. Restart the IDE afterwards.# Windows: Documents\Arduino\libraries\DHT_sensor_library\DHT.h # macOS: ~/Documents/Arduino/libraries/DHT_sensor_library/DHT.h # Linux: ~/Arduino/libraries/DHT_sensor_library/DHT.h -
File from your own project: quotes and exact case
A
.hplaced next to the.inois included with quotes, and its name must have exactly the same case as the file.#include <DHT.h> // installed library #include "config.h" // file in the sketch folder -
Turn on verbose output if it still fails
In File > Preferences, tick "Show verbose output during compile". The
Using library … at version … in folder:andMultiple libraries were found forlines show which library is really used.
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Write at the top of each sketch the required libraries with their exact Library Manager name and version.
- For a shared project, use PlatformIO or an arduino-cli
sketch.yaml: dependencies are declared there and installed automatically. - Select the board before opening an example: the examples and libraries offered depend on the selected board.
Frequently asked questions
I installed the library, why does the error persist?
Often the missing file is a dependency (for example Adafruit_Sensor.h), or several versions of the library coexist. Read the exact name in the error and turn on verbose output to see the folder used.
What is the difference with "was not declared in this scope"?
Here, the .h file cannot be found and compilation stops at the #include. "was not declared in this scope" comes later: the file is included, but a function or variable is not known at that point.
What does "exit status 1 Error compiling for board" mean?
It is only the IDE summary: compilation failed. The real error is higher up in the console, on the first line containing error:.
Useful tools for this error
More Electronics & Arduino errors
- Fix "avrdude: stk500_getsync(): not in sync" on Arduino
- Fix "Failed to connect to ESP32" (Wrong boot mode, No serial data received)
- Fix "Brownout detector was triggered" on ESP32
- Fix "can't open device /dev/ttyUSB0: Permission denied" (Arduino, ESP32)
- Fix "Guru Meditation Error: Core 1 panic'ed (LoadProhibited)" on ESP32
- Fix "error: 'x' was not declared in this scope" (Arduino, C++)
- Fix "Sketch too big" on Arduino and ESP32 (flash memory full)
- Fix "OSError: [Errno 121] Remote I/O error" with I2C on Raspberry Pi