esp: ESP-IDF VSCode Configuration

环境介绍:

  • VSCode:v1.92.2
  • C/C++ 扩展
  • nRF Kconfig 扩展
  • CMake Tools 扩展
  • Makefile Tools 扩展

为什么我明明已经安装了 esp-idf 环境,但是 VSCode 打开工程后,编程却没有 esp-idf 函数、变量、宏等 智能提示。但是我发现对于GNU标准库 glibc 中头文件的函数可以出现智能提示。例如:stdio.h、stdlib.h、time.h、math.h、assert.h

VSCode C/C++ 扩展 能够对于GNU标准库的智能提示,原因是:默认自动加载了GCC头文件路径。例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 通过命令查询 GCC 的配置信息,包括头文件路径
$ gcc -E -x c++ - -v < /dev/null
省略部分...
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/11
/usr/include/x86_64-linux-gnu/c++/11
/usr/include/c++/11/backward
/usr/lib/gcc/x86_64-linux-gnu/11/include
/usr/local/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
省略部分...

其中 /usr/include 就是 GNU标准库的C相关头文件路径,/usr/include/c++/11 就是GNU标准库的C++相关头文件路径。

C相关的标准库头文件

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
27
28
29
30
31
32
33
$ caojun@caojun-NMH-WCX9:~$ ls /usr/include/

aio.h drm getopt.h misc paths.h scsi thread_db.h
aliases.h elf.h gio-unix-2.0 mntent.h pcre2.h search.h threads.h
alloca.h endian.h GL monetary.h pcre2posix.h selinux tic.h
argp.h envz.h glib-2.0 mosquitto.h pcrecpparg.h semaphore.h time.h
argz.h err.h glob.h mqtt_protocol.h pcrecpp.h sepol tirpc
ar.h errno.h gnumake.h mqueue.h pcre.h setjmp.h ttyent.h
arpa error.h gnu-versions.h mtd pcreposix.h sgtty.h uchar.h
asm-generic eti.h gobject-introspection-1.0 nc_tparm.h pcre_scanner.h shadow.h ucontext.h
assert.h etip.h grp.h ncurses_dll.h pcre_stringpiece.h signal.h ulimit.h
avahi-client execinfo.h gshadow.h ncurses.h pixman-1 sound unctrl.h
avahi-common expat_external.h iconv.h ncursesw pngconf.h spawn.h unistd.h
blkid expat.h ifaddrs.h net png.h stab.h utime.h
brotli fcntl.h inttypes.h netash pnglibconf.h stdc-predef.h utmp.h
byteswap.h features.h iproute2 netatalk poll.h stdint.h utmpx.h
c++ features-time64.h langinfo.h netax25 printf.h stdio_ext.h uuid
cairo fenv.h lastlog.h netdb.h proc_service.h stdio.h values.h
complex.h finclude libgen.h neteconet protocols stdlib.h video
cpio.h FlexLexer.h libintl.h netinet pthread.h string.h wait.h
crypt.h fmtmsg.h libmount netipx pty.h strings.h wchar.h
ctype.h fnmatch.h libpng netiucv pwd.h sudo_plugin.h wctype.h
cursesapp.h fontconfig libpng16 netpacket python3.10 syscall.h wordexp.h
cursesf.h form.h limits.h netrom rdma sysexits.h X11
curses.h freetype2 link.h netrose readline syslog.h x86_64-linux-gnu
cursesm.h fstab.h linux nfs re_comp.h tar.h xcb
cursesp.h fts.h locale.h nl_types.h regex.h termcap.h xen
cursesw.h ftw.h malloc.h nss.h regexp.h term_entry.h xorg
cursslk.h gcalc-2 math.h obstack.h resolv.h term.h zconf.h
dbus-1.0 gci-2 mcheck.h openssl rpc termio.h zlib.h
dirent.h gconv.h memory.h openvpn rpcsvc termios.h
dlfcn.h gdb menu.h panel.h sched.h tgmath.h

C++标准库相关的头文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ ls /usr/include/c++/11/
algorithm cfenv concepts ctgmath fenv.h latch ostream span tr1
any cfloat condition_variable ctime filesystem limits parallel sstream tr2
array charconv coroutine cuchar forward_list list pstl stack tuple
atomic chrono csetjmp cwchar fstream locale queue stdexcept typeindex
backward cinttypes csignal cwctype functional map random stdlib.h typeinfo
barrier ciso646 cstdalign cxxabi.h future math.h ranges stop_token type_traits
bit climits cstdarg debug initializer_list memory ratio streambuf unordered_map
bits clocale cstdbool decimal iomanip memory_resource regex string unordered_set
bitset cmath cstddef deque ios mutex scoped_allocator string_view utility
cassert codecvt cstdint exception iosfwd new semaphore syncstream valarray
ccomplex compare cstdio execution iostream numbers set system_error variant
cctype complex cstdlib experimental istream numeric shared_mutex tgmath.h vector
cerrno complex.h cstring ext iterator optional source_location thread version

对于 esp-idf 相关的头文件需要用户自定义配置文件,让VSCode 能够找到 esp-idf 的头文件。

在当前项目的同一级创建 .vscode 目录,在该目录下创建这个文件: c_cpp_properties.json

  • settings.json

    该文件是默认创建的

  • c_cpp_properties.json

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    {
    "configurations": [
    {
    "name": "ESP-IDF",
    "includePath": [
    "${workspaceFolder}/**",
    "/home/your_username/esp/esp-idf/components/**"
    ]
    }
    ],
    "version": 4
    }

    注意:/home/your_username/esp/esp-idf/components/** 根据 esp-idf 实际安装路径