ESP-IDF使用总结&避坑指南

文章介绍了在使用VSCode进行ESP32开发时遇到的头文件路径问题及其解决方案,强调了正确配置CMakeLists.txt文件以避免component项目构建错误。同时,讨论了分区表的重要性,特别是如何处理因分区空间不足导致的编译错误,并提到了SPIFFS文件系统的分区需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

vscode提示找不到头文件路径

component项目构建错误导致编译失败

分区表的二三事


vscode提示找不到头文件路径

当我们直接用vscode打开别人的工程文件时

esp32相关的头文件都是提示找不到路径的,虽然问题不大但是不能直接跳转了,也不方便调试

解决方法:按Ctrl+Shift+P,打开命令窗口,选择添加vscode配置文件夹

(第一次使用的话需要手动输入一下才能找到)

component项目构建错误导致编译失败

espidf会默认把component文件夹下所有文件的路径包含进去,但是在component文件夹下构建自己的项目需要自己创建CMakeLists.txt文件,不然就会出现虽然文件能正常跳转但是编译的时候就会提示找不到xx文件的玄学问题

fatal error: lvgl_init.h: No such file or directory

示例项目目录结构如下:(图源ESP-IDF编程指南)

CMakeList文件主要包含一下几个部分:

SRCS:源文件列表(*.c、*.cpp、*.cc、*.S),里面所有的源文件都将会编译进组件库中

不同源文件中间以空格分隔

 INCLUDE_DIRS:里面的路径会被添加到所有需要该组件的组件(包括 main 组件)全局 include 搜索路径中

列出头文件所在文件夹的名称,如果头文件与c源文件在同一路径下则跟 "." 即可

REQUIRES:用来声明该组件需要使用哪些其它组件

举例一,所有文件都在main文件夹里:

 main文件夹里的目录结构如下:

CMakeLists.txt文件示例如下:

举例二,头文件在main/include路径下:

 对应的CmakeList.txt如下:

举例三,构建了components文件夹目录,其中lv_port依赖于lvgl和lvgl_esp32_drivers:

正确CMakeLists.txt文件示例如下:

如果缺少CMakeList文件报错如下:

../main/app_main.c:14:10: fatal error: lvgl_init.h: No such file or directory
 #include "lvgl_init.h"

分区表的二三事

我们在编译一个项目的时候关于分区表通常有如下几个选项:

 而分区表简单理解来就是对芯片的flash存储空间的一个划分,每片 ESP32的 flash 可以包含多个应用程序,以及多种不同类型的数据(例如校准数据、文件系统数 据、参数存储数据等)

默认选项Single factory app实际上是这样的

factory (0x00) 是默认的 app 分区。启动加载器将默认加载该应用程序。而且偏移位置必须在0x10000处,否则会报错

(但如果存在类型为 data/ota 分区,则启动加载器将加载 data/ota 分区中的数据,进而判断启动哪个 OTA 镜像文件)但我目前是没有用到这些的

实际上大部分情况下修改factory的大小分给程序足够的存储空间即可,因为默认情况下只给factory分配了1M的内存,而我们可以选购拥有8MB、16MB的flash大小的芯片

而当划分给factory的空间不足时会有如下的编译错误:

Error: app partition is too small for binary main.bin size 0x228400:
  - Part 'factory' 0/0 @ 0x10000 size 0x100000 (overflow 0x128400)
ninja: build stopped: subcommand failed.

这个时候就需要我们选择第二个选项或者直接自定义分区表了

如果我们使用spiffs的话,也需要自己定义分区表并加上spiffs的大小划分

可以直接修改Size大小,offset偏移量编译时会自动计算

另外使用spiffs的话main文件的CMakeList文件也需要修改,对小白来说算是一个坑了

(问就是我也踩过)

 另外贴一张官方文档对其余部分的解释,文本不做详细介绍

参考视频&文章

【分区 Partition Table - 乐鑫 ESP32 物联网开发框架 ESP-IDF 开发入门 - 孤独的二进制出品】【自定义菜单 Menuconfig - 乐鑫 ESP32 物联网开发框架 ESP-IDF 开发入门 - 孤独的二进制出品】https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32s3/get-started/index.html

笔者水平有限,仅作分享记录,欢迎批评指正(手动笔芯)

esp32-freertos-sdk 工具包 See the Getting Started guide links above for a detailed setup guide. This is a quick reference for common commands when working with ESP-IDF projects: Setup Build Environment (See Getting Started guide for a full list of required steps with details.) Install host build dependencies mentioned in Getting Started guide. Add tools/ directory to the PATH Run python -m pip install -r requirements.txt to install Python dependencies Configuring the Project idf.py menuconfig Opens a text-based configuration menu for the project. Use up & down arrow keys to navigate the menu. Use Enter key to go into a submenu, Escape key to go out or to exit. Type ? to see a help screen. Enter key exits the help screen. Use Space key, or Y and N keys to enable (Yes) and disable (No) configuration items with checkboxes "[*]" Pressing ? while highlighting a configuration item displays help about that item. Type / to search the configuration items. Once done configuring, press Escape multiple times to exit and say "Yes" to save the new configuration when prompted. Compiling the Project idf.py build ... will compile app, bootloader and generate a partition table based on the config. Flashing the Project When the build finishes, it will print a command line to use esptool.py to flash the chip. However you can also do this automatically by running: idf.py -p PORT flash Replace PORT with the name of your serial port (like COM3 on Windows, /dev/ttyUSB0 on Linux, or /dev/cu.usbserial-X on MacOS. If the -p option is left out, idf.py flash will try to flash the first available serial port. This will flash the entire project (app, bootloader and partition table) to a new chip. The settings for serial port flashing can be configured with idf.py menuconfig. You don't need to run idf.py build before running idf.py flash, idf.py flash will automatically rebuild anything which needs it. Viewing Serial Output The idf.py monitor target uses the idf_monitor tool to display se
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值