How to fix ESP-IDF fatal error: cJSON.h: No such file or directory

Problem

When building an ESP-IDF project, you may encounter the following error:

example.txt
/home/user/MyProject/main/main.cpp:10:10: fatal error: cJSON.h: No such file or directory
   10 | #include <cJSON.h>
      |          ^~~~~~~~~
compilation terminated.

Solution

You need to ensure that the json component is included in your project. This can be done by modifying your main/CMakeLists.txt file to include the json component as a dependency.

You can do this by adding the REQUIRES json line in your idf_component_register function. Here’s how to modify your main/CMakeLists.txt file:

example.txt
```cmake {filename="idf_component_register_json.cmake"}
idf_component_register(
  SRCS "main.cpp"
  INCLUDE_DIRS "../include"
  REQUIRES json
)

Check out similar posts by category: ESP-IDF, ESP32