测试环境#
Ubuntu 22.04
1
2
3
4
5
6
| $ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.5 LTS
Release: 22.04
Codename: jammy
|
安装 ESP-IDF#
安装 EIM(ESP-IDF 安装管理器)
1
2
3
4
5
6
7
| # APT 源列表添加 EIM 仓库
$ echo "deb [trusted=yes] https://dl.espressif.com/dl/eim/apt/ stable main" | sudo tee /etc/apt/sources.list.d/espressif.list
$ sudo apt update
# 这里仅安装 CLI
$ sudo apt install eim-cli
|
更新 EIM(可选)
1
| sudo apt update && sudo apt upgrade eim-cli
|
卸载 EIM(可选)
1
| sudo apt remove eim-cli
|
安装 ESP-IDF#
这里使用当前最新的 ESP-IDF 稳定版本(v6.0.1)。
1
2
3
4
5
6
7
8
9
10
| $ sudo eim install -i v6.0.1
...
2026-05-30 22:20:42 - 10 - 05 - INFO - Wizard result: Ok
2026-05-30 22:20:42 - 10 - 05 - INFO - Successfully installed IDF
2026-05-30 22:20:42 - 10 - 05 - INFO - Now you can start using IDF tools
$ eim list
2026-05-30 22:21:25 - 10 - 05 - INFO - Listing installed versions...
Installed versions:
- v6.0.1 (selected) [/home/zeepunt/.espressif/v6.0.1/esp-idf]
|
默认情况下,ESP-IDF 及其相关组件是安装到 ~/.espressif 目录下。
查看 ESP-IDF 的启动脚本
1
2
3
4
5
6
7
8
| $ eim select
Available versions:
✔ Which version do you want to select? · v6.0.1
Selected version: v6.0.1
===========================================
To activate this environment, run the following command in your terminal:
source "/home/zeepunt/.espressif/tools/activate_idf_v6.0.1.sh"
===========================================
|
安装依赖#
1
| $ sudo apt install libbsd-dev
|
构建和编译项目#
假设当前并不存在 ESP-IDF 项目,所以我们要新建一个。
1
2
3
4
5
6
| # 启动 ESP-IDF 环境
$ source ~/.espressif/tools/activate_idf_v6.0.1.sh
# 创建项目
$ idf.py create-project esp_linux
$ cd esp_linux
|
启动 ESP-IDF 环境后,可以使用命令 deactivate 退出。
修改该项目根目录下的 CMakeLists.txt 文件。
1
2
3
4
5
6
7
8
9
10
11
| $ vim CMakeLists.txt
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.22)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(COMPONENTS main) # 添加这行, 而且必须放在 project 前面
project(esp_linux)
|
将目标芯片设为 linux。
1
2
3
4
5
6
| $ idf.py --preview set-target linux
# 查看是否配置成功
$ cat sdkconfig | grep CONFIG_IDF_TARGET
CONFIG_IDF_TARGET="linux"
CONFIG_IDF_TARGET_LINUX=y
|
编译。
运行。
1
2
3
4
5
6
7
8
| $ idf.py monitor
Executing action: monitor
Running idf_monitor in directory /home/zeepunt/share/project/esp_linux
Executing "/home/zeepunt/.espressif/tools/python/v6.0.1/venv/bin/python /home/zeepunt/.espressif/v6.0.1/esp-idf/tools/idf_monitor.py --toolchain-prefix --target linux --revision 0 /home/zeepunt/share/project/esp_linux/build/esp_linux.elf -m '/home/zeepunt/.espressif/tools/python/v6.0.1/venv/bin/python' '/home/zeepunt/.espressif/v6.0.1/esp-idf/tools/idf.py'"...
--- esp-idf-monitor 1.9.0 on linux
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
I (13595519) port: Starting scheduler.
|