环境搭建 VSCode
2026-06-26
- 通过GCC编译器、MinGW、CMake构建工具和OpenOCD调试工具实现vscode开发环境
- 支持编译、调试、下载,变量查看,寄存器查看,断点仿真
- 支持但不限于STM32、GD32等支持交叉编译的单片机
安装GCC编译器
交叉编译器在一种平台上编译代码,在另一种平台上运行。 gcc-arm-none-eabi是ARM Cortex-M系列微控制器的交叉编译器,支持C和C++语言的编译。它可以将源代码编译为适用于ARM架构的可执行文件或固件。
直接默认安装即可,安装路径建议不要有中文和空格,默认安装路径为C:\Program Files (x86)\GNU Tools Arm Embedded\10 2021.10
最后安装后勾选
Add path to environment variable选项,这样就不需要手动添加环境变量。完成后切回桌面输入
win + r,输入cmd打开命令行,输入arm-none-eabi-gcc -v查看是否安装成功。
成功后输出内容如下
C:\Users\user>arm-none-eabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-none-eabi-gcc
COLLECT_LTO_WRAPPER=c:/program\ files\ (x86)/gnu\ arm\ embedded\ toolchain/10\ 2021.10/bin/../lib/gcc/arm-none-eabi/10.3.1/lto-wrapper.exe
Target: arm-none-eabi
Configured with: /mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/src/gcc/configure --build=x86_64-linux-gnu --host=i686-w64-mingw32 --target=arm-none-eabi --prefix=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/install-mingw --libexecdir=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/install-mingw/lib --infodir=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/install-mingw/share/doc/gcc-arm-none-eabi/info --mandir=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/install-mingw/share/doc/gcc-arm-none-eabi/man --htmldir=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/install-mingw/share/doc/gcc-arm-none-eabi/html --pdfdir=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/install-mingw/share/doc/gcc-arm-none-eabi/pdf --enable-languages=c,c++ --enable-mingw-wildcard --disable-decimal-float --disable-libffi --disable-libgomp --disable-libmudflap --disable-libquadmath --disable-libssp --disable-libstdcxx-pch --disable-nls --disable-shared --disable-threads --disable-tls --with-gnu-as --with-gnu-ld --with-headers=yes --with-newlib --with-python-dir=share/gcc-arm-none-eabi --with-sysroot=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/install-mingw/arm-none-eabi --with-libiconv-prefix=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/build-mingw/host-libs/usr --with-gmp=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/build-mingw/host-libs/usr --with-mpfr=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/build-mingw/host-libs/usr --with-mpc=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/build-mingw/host-libs/usr --with-isl=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/build-mingw/host-libs/usr --with-libelf=/mnt/workspace/workspace/GCC-10-pipeline/jenkins-GCC-10-pipeline-338_20211018_1634516203/build-mingw/host-libs/usr --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --with-pkgversion='GNU Arm Embedded Toolchain 10.3-2021.10' --with-multilib-list=rmprofile,aprofile
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 10.3.1 20210824 (release) (GNU Arm Embedded Toolchain 10.3-2021.10)
C:\Users\user>安装Make工具
通过交叉编译工具已经能够编译文件,但参数繁琐通过Makefile文件可以简化编译过程。Make工具是一个自动化构建工具,它读取Makefile文件中的规则和指令,自动执行gcc编译、链接等操作,从而生成可执行文件或库文件。
- 下载地址:x86_64-16.1.0-release-posix-seh-ucrt-rt_v14-rev1.7z
- 解压后将
mingw64复制到C:\Program Files\目录下,路径为C:\Program Files\mingw64 - 将
C:\Program Files\mingw64\bin\mingw32-make.exe重命名为make.exe - 将
C:\Program Files\mingw64\bin添加到系统环境变量Path中win + e打开文件资源管理器,右键此电脑,选择属性- 点击高级系统设置,点击环境变量
- 在系统变量中找到Path,点击编辑,点击新建,输入
C:\Program Files\mingw64\bin,点击确定保存
- 完成后切回桌面输入
win + r,输入cmd打开命令行,输入make -v查看是否安装成功。
成功后输出内容如下
C:\Users\user>make -v
GNU Make 4.4.1
Built for x86_64-w64-mingw32
Copyright (C) 1988-2023 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
C:\Users\user>安装CMake工具
通过Makefile已经能够简化编译过程,但Makefile文件的编写和维护可能比较复杂。CMake是一种跨平台的构建系统,它使用CMakeLists.txt文件来描述项目的构建规则。CMake可以自动生成适用于不同平台和编译器的Makefile或项目文件,再一次简化构建过程。
- 下载地址:cmake-4.4.0-rc2-windows-x86_64.msi
- 直接默认安装即可,安装中选项选择
Add CMake to the PATH environment variable,这样就不需要手动添加环境变量。 - 默认安装路径为
C:\Program Files\CMake - 完成后切回桌面输入
win + r,输入cmd打开命令行,输入cmake -version查看是否安装成功。
成功后输出内容如下
C:\Users\user>cmake -version
cmake version 4.4.0-rc2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
C:\Users\user>安装OpenOCD调试工具
openOCD是一个开源的调试工具,它提供了对嵌入式系统的调试和编程支持。它可以与各种调试器(如ST-LINK、J-Link等)配合使用,实现对目标设备的调试、下载和烧录操作。OpenOCD支持多种调试协议,如JTAG和SWD,能够与GDB(GNU Debugger)集成,实现源代码级调试。
- 下载地址:openocd-v0.12.0-i686-w64-mingw32.tar.gz
- 解压后将
openocd-0.12.0-i686-w64-mingw32复制到C:\Program Files\目录下,路径为C:\Program Files\openocd-0.12.0-i686-w64-mingw32 - 将
C:\Program Files\openocd-0.12.0-i686-w64-mingw32\bin添加到系统环境变量Path中win + e打开文件资源管理器,右键此电脑,选择属性- 点击高级系统设置,点击环境变量
- 在系统变量中找到Path,点击编辑,点击新建,输入
C:\Program Files\openocd-0.12.0-i686-w64-mingw32\bin,点击确定保存
- 完成后切回桌面输入
win + r,输入cmd打开命令行,输入openocd -v查看是否安装成功。
C:\Users\user>openocd -v
Open On-Chip Debugger 0.12.0 (2023-01-14-23:37)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
C:\Users\user>安装VSCODE
vscode是一个轻量级的代码编辑器,它提供了丰富的插件生态系统和强大的调试功能。通过安装相关插件,vscode可以成为一个功能强大的集成开发环境(IDE),支持多种编程语言和开发工具。
- 下载地址:VSCodeUserSetup-x64-1.81.1.exe
- 直接默认安装即可,安装中选项选择
Add to PATH,这样就不需要手动添加环境变量。
需要安装的插件,左侧栏点击扩展,搜索安装以下插件:
- C/C++:提供C/C++语言的语法高亮、智能提示和调试功能。
- Cortex-Debug:提供对ARM Cortex-M微控制器的调试支持,支持多种调试器和调试协议。
- CMake: 提供对CMake构建系统的支持,能够解析CMakeLists.txt文件并生成相应的构建任务。
- Chinese (Simplified) Language Pack for Visual Studio Code:提供中文界面支持,方便中文用户使用vscode。
测试
以STM32F103C8T6标准库工程为例,进行编译、下载、仿真。
拖拽实例工程文件夹到vscode中
编译
- 首次构建、执行清空后、增删文件均需要重新执行
终端->运行任务->cmake已生成描述文件之间关联等参数的Makefile文件, - 单独修改文件直接执行
终端->运行任务->make即可,或快捷键CTRL + SHIFT + B
成功重新构建后镜像输出:
build/app.elf是仿真器调试时使用的可执行文件build/app.bin是download任务下载到芯片时所用的的二进制文件。build/bin/demo.binbuild/bin/demo.hex通过项目名称复制出来的供外部烧录。
全部重新编译
重新编译为cmake与make的串行执行集合
标题栏选择终端->运行任务->rebuild
成功编译输出如下
* 正在执行任务: rm * -r
* 终端将被任务重用,按任意键关闭。
* 正在执行任务: cmake -G 'MinGW Makefiles' ..
-- The C compiler identification is GNU 16.1.0
-- The CXX compiler identification is GNU 16.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Program Files/mingw64/bin/cc.exe
-- Check for working C compiler: C:/Program Files/mingw64/bin/cc.exe - works
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - failed
-- Check for working CXX compiler: C:/Program Files/mingw64/bin/cc.exe
-- Check for working CXX compiler: C:/Program Files/mingw64/bin/cc.exe - works
-- Detecting CXX compile features
-- Detecting CXX compile features - done
build for debug!
-- The ASM compiler identification is GNU
-- Found assembler: C:/Program Files (x86)/GNU Arm Embedded Toolchain/10 2021.10/bin/arm-none-eabi-gcc.exe
-- Configuring done (5.3s)
-- Generating done (0.1s)
-- Build files have been written to: C:/Users/user/Desktop/project/build
* 终端将被任务重用,按任意键关闭。
* 正在执行任务: make
[ 3%] Building C object CMakeFiles/app.elf.dir/main.c.obj
[ 7%] Building C object CMakeFiles/app.elf.dir/CMSIS/CM3/DeviceSupport/ST/STM32F10x/startup/gcc_ride7/startup_stm32f10x_md.s.obj
[ 10%] Building C object CMakeFiles/app.elf.dir/CMSIS/CM3/CoreSupport/core_cm3.c.obj
[ 14%] Building C object CMakeFiles/app.elf.dir/CMSIS/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.c.obj
[ 17%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/misc.c.obj
[ 21%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_adc.c.obj
[ 25%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_bkp.c.obj
[ 28%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_can.c.obj
[ 32%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_cec.c.obj
[ 35%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_crc.c.obj
[ 39%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_dac.c.obj
[ 42%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_dbgmcu.c.obj
[ 46%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_dma.c.obj
[ 50%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_exti.c.obj
[ 53%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c.obj
[ 57%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_fsmc.c.obj
[ 60%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c.obj
[ 64%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c.obj
[ 67%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_iwdg.c.obj
[ 71%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_pwr.c.obj
[ 75%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_rcc.c.obj
[ 78%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_rtc.c.obj
[ 82%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_sdio.c.obj
[ 85%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_spi.c.obj
[ 89%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_tim.c.obj
[ 92%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c.obj
[ 96%] Building C object CMakeFiles/app.elf.dir/STM32F10x_StdPeriph_Driver/src/stm32f10x_wwdg.c.obj
[100%] Linking C executable app.elf
Building demo (bin, hex, elf) and print size
text data bss dec hex filename
1172 8 1568 2748 abc C:/Users/user/Desktop/project/build/app.elf
0 1180 0 1180 49c C:/Users/user/Desktop/project/build/app.hex
[100%] Built target app.elf
* 终端将被任务重用,按任意键关闭。下载
- 连接目标板
- 标题栏选择
终端->运行任务->download
成功下载输出如下
* 正在执行任务: make
[100%] Built target app.elf
* 终端将被任务重用,按任意键关闭。
* 正在执行任务: openocd -f ../stlink-dap.cfg -f ../stm32f1x.cfg -c init -c halt -c 'program app.bin 0x8000000' -c reset -c shutdown
Open On-Chip Debugger 0.12.0 (2023-01-14-23:37)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "dapdirect_swd". To override use 'transport select <transport>'.
Info : STLINK V2J37S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 2.400000
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : stlink_dap_op_connect(connect)
Info : SWD DPIDR 0x1ba01477
Info : [stm32f1x.cpu] Cortex-M3 r1p1 processor detected
Info : [stm32f1x.cpu] target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for stm32f1x.cpu on 3333
Info : Listening on port 3333 for gdb connections
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x81000000 pc: 0x08000152 msp: 0x20004ff0
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x080001c0 msp: 0x20005000
** Programming Started **
Info : device id = 0x20036410
Info : flash size = 64 KiB
Warn : Adding extra erase range, 0x0800049c .. 0x080007ff
** Programming Finished **
shutdown command invoked
* 终端将被任务重用,按任意键关闭。下载快捷键
添加 Ctrl + Shift + D 为执行download任务的快捷键
- 按下
Ctrl + Shift + P,输入Preferences: Open Keyboard Shortcuts (JSON)并回车。 - 注意不要选成
Preferences: Open Default Keyboard Shortcuts (JSON)只读文件 - 在打开的 JSON 数组中,修改为如下内容:
[
// ... 你可能已有其他快捷键配置
{
"key": "ctrl+shift+d",
"command": "workbench.action.tasks.runTask",
"args": "download"
}
]仿真
直接F5 或者 侧边连切换运行和调试->点击绿色图标
成功仿真后如下
Waiting for gdb server to start...[2026-07-03T06:44:28.165Z] SERVER CONSOLE DEBUG: onBackendConnect: gdb-server session connected. You can switch to "DEBUG CONSOLE" to see GDB interactions.
openocd.exe -c "gdb_port 50000" -c "tcl_port 50001" -c "telnet_port 50002" -s "C:\\Users\\user\\Desktop\\project" -f "c:/Users/user/.vscode/extensions/marus25.cortex-debug-1.12.1/support/openocd-helpers.tcl" -f "C:\\Users\\user\\Desktop\\project/stlink-dap.cfg" -f "C:\\Users\\user\\Desktop\\project/stm32f1x.cfg"
Open On-Chip Debugger 0.12.0 (2023-01-14-23:37)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
CDLiveWatchSetup
Info : auto-selecting first available session transport "dapdirect_swd". To override use 'transport select <transport>'.
Info : Listening on port 50001 for tcl connections
Info : Listening on port 50002 for telnet connections
Info : STLINK V2J37S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 2.400000
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : Unable to match requested speed 1000 kHz, using 950 kHz
Info : clock speed 950 kHz
Info : stlink_dap_op_connect(connect)
Info : SWD DPIDR 0x1ba01477
Info : [stm32f1x.cpu] Cortex-M3 r1p1 processor detected
Info : [stm32f1x.cpu] target has 6 breakpoints, 4 watchpoints
Info : starting gdb server for stm32f1x.cpu on 50000
Info : Listening on port 50000 for gdb connections
Info : accepting 'gdb' connection on tcp/50000
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x81000000 pc: 0x0800015a msp: 0x20004ff0
Info : device id = 0x20036410
Info : flash size = 64 KiB
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x080001c0 msp: 0x20005000
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x080001c0 msp: 0x20005000
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x080001c0 msp: 0x20005000
[stm32f1x.cpu] halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x080001c0 msp: 0x20005000F5继续F10逐过程F11单步
左侧运行和调试栏中的 XPERIPHERALS 可查看寄存器变量