1| Supported Targets | ESP32 |
2| ----------------- | ----- |
3
4# Using ESP-IDF in Custom CMake Projects
5
6This example illustrates using ESP-IDF components as libraries in custom CMake projects. The application
7in this example can run on either host or on an ESP32, and the appropriate libraries are linked
8to the executable depending on which target is specified. If the target is an ESP32, the libraries
9created from ESP-IDF components are linked. On the other hand, stub libraries are linked if example
10is meant to be run on the host to simulate the same application behavior.
11
12The application in this example is equivalent to the `hello_world` example under `examples/get-started/hello_world`.
13
14## Example Flow
15
16Users looking at this example should focus on the [top-level CMakeLists.txt file](./CMakeLists.txt). This builds an
17application that can run on the target without relying on the typical ESP-IDF application template.
18
19### Output
20
21```
22Hello world!
23This is ESP32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 0, 4MB external flash
24Restarting in 10 seconds...
25Restarting in 9 seconds...
26Restarting in 8 seconds...
27Restarting in 7 seconds...
28Restarting in 6 seconds...
29Restarting in 5 seconds...
30Restarting in 4 seconds...
31Restarting in 3 seconds...
32Restarting in 2 seconds...
33Restarting in 1 seconds...
34Restarting in 0 seconds...
35```
36
37## Building this Example
38
39To build this example, the user can either run [build-esp32.sh](./build-esp32.sh) to build for the ESP32
40or run [build.sh](./build.sh) to build for the host:
41
42```bash
43# Builds the example for ESP32
44./build-esp32.sh
45```
46
47Note: To build for a different target SoC, copy the `build-esp32.sh` file and change the `-DTARGET=esp32` clause on the second line.
48
49or
50
51```bash
52# Builds the example to run on host
53./build.sh
54```
55
56## Flashing and Running this Example
57
58To flash and run the example, users can run either  [run-esp32.sh](./run-esp32.sh) or [run.sh](./run.sh) depending
59on what the example was built for. In the case of ``run-esp32.sh``, the port needs to be specified:
60
61```bash
62# Run the example on device connected to /dev/ttyUSB1
63./run-esp32.sh /dev/ttyUSB1
64```
65
66or
67
68```bash
69# Run the example on the host
70./run.sh
71```
72
73## Configuring this Example
74
75To modify the example ESP-IDF project configuration, first create the CMake build directory. This can be done by running `build-esp32.sh` or by running only the first two lines in `build-esp32.sh` (which won't build the actual project yet).
76
77Then execute the menuconfig build target in the build directory:
78
79```bash
80cmake --build build -- menuconfig
81```
82
83If using ninja directly:
84
85```bash
86ninja -C build menuconfig
87```
88
89Note: ESP-IDF project configuration isn't used by the host CMake builds, the config is only read when the project is built using the ESP-IDF build system.
90
91---
92
93There is a discussion on using ESP-IDF in custom CMake projects in the programming guide under `API Guides` -> `Build System` -> `Using ESP-IDF in Custom CMake Projects`
94