参考官方文档
Linux (Ubuntu)
-
默认软件包安装:
sudo apt-get install git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
-
获取 ESP-IDF
mkdir -p ~/esp cd ~/esp git clone --recursive https://github.com/espressif/esp-idf.git
-
获取工具链
cd ~/esp/esp-idf ./install esp32
执行 ./install.sh 时, 可以安装指定芯片的工具, 比如 esp32s2 或 all.
下载默认位置为 $HOME/.espressif, 可在下载前设置 IDF_TOOLS_PATH 以更改位置 -
导出环境
source $HOME/esp/esp-idf/export.sh
执行 esp-idf 目录下的
export.sh
可使得开发环境在当前终端生效, 可以更改 ~/.bashrc 打开终端总是生效或者使用 alias 按需生效:export IDF_PATH=/home/shino/esp/esp-idf alias esp="source $IDF_PATH/export.sh"
这时只要在任意终端输入 esp 即可进入编译环境.
工程示例
- 创建工程
cp -r ~/esp/esp-idf/examples/get-started/hello_world . cd hello_world mkdir -p components/my_component cd components/my_component touch my_component.c my_component.h CMakeLists.txt
上面操作复制了官方例程中的 hello_world 作为模板, 然后创建了一个组件 my_component.
- 编写组件
my_component.c
my_component.h#include "my_component.h" int add(int a, int b) { return a + b; }
my_component/CMakeLists.txt#pragma once int add(int a, int b);
main/CMakeLists.txtidf_component_register(SRCS "my_component.c" INCLUDE_DIRS ".")
idf_component_register(SRCS "hello_world_main.c" INCLUDE_DIRS "" PRIV_REQUIRES my_component)
组件之间的依赖关系可在组件注册时使用 PRIV_REQUIRES 或 REQUIRES 来表示, 其中前者表示私有依赖, 后者则为共有依赖。例如上面因为在 main.c 中 include 了 my_component.h 说明 main 对my_component有依赖关系, 而 main 本身可看做是一个特殊的组件, 因为没有组件依赖它且其对my_component组件的依赖没有体现在头文件上因此可使用
PRIV_REQUIRES
来申明。
更多关于 ESP CMakeLists.txt 构建系统的内容参阅官网文档