ESP-IDF + VScode 开发环境搭建BUG

前言

在 VScode 上使用 Espressif 重新搭建 ESP32 开发环境时,虽然能正常编译和烧录,但出现问题如下:

  1. 终端提示 “无法使用 compilepath 解析配置”,打开 C/C++ 扩展发现找不到编译器路径。
  2. VScode 找不到头文件,波浪线警告。

解决方法

编译器路径报错

打开 C/C++ 扩展显示找不到路径${env:IDF_TOOLS_PATH}/tools/xtensa-esp32-elf/esp-12.2.0_20230208/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc.exe
其中 ${env:IDF_TOOLS_PATH} 是 idf tools 在环境变量中的路径,去环境变量里面没有找到,所以直接新建环境变量如下,与实际安装的idf tools位置对应好。

头文件报错

打开工程目录下 .vscode目录中的 c_cpp_properties.json,里面描述的是对应的头文件路径,内容如下所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
"configurations": [
{
"name": "ESP-IDF",
"compilerPath": "${env:IDF_TOOLS_PATH}/tools/xtensa-esp32-elf/esp-12.2.0_20230208/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc.exe",
"includePath": [
"${config:idf.espIdfPath}/components/**",
"${config:idf.espIdfPathWin}/components/**",
"${config:idf.espAdfPath}/components/**",
"${config:idf.espAdfPathWin}/components/**",
"${workspaceFolder}/**"
],
"browse": {
"path": [
"${config:idf.espIdfPath}/components",
"${config:idf.espIdfPathWin}/components",
"${config:idf.espAdfPath}/components/**",
"${config:idf.espAdfPathWin}/components/**",
"${workspaceFolder}/**"
],
"limitSymbolsToIncludedHeaders": false
}
}
],
"version": 4
}

更改环境变量后,显示找不到"${config:idf.espAdfPath}/components/**""${config:idf.espAdfPathWin}/components/**"路径,查询资料发现这两个属于 ESP-ADF,是乐鑫基于 IDF 开发的音频开发框架,反正现在也用不上,就直接删掉这两个路径。
处理完上面问题后,又出现未定义标识符CONFIG_IDF_TARGETportTICK_PERIOD_MS,但是仍然能够正常跳转定义和编译,在头文件添加下述头文件解决问题。

1
#include "../build/config/sdkconfig.h"

之前配置时没这么多问题,猜想可能是没有安装在默认盘符导致的。

参考链接

  1. 环境变量
  2. 未定义标识符