VSCode IDE 配置环境过程解析

来自:网络
时间:2022-08-07
阅读:
目录

如果用 PlatformIO 创建 libopencm3 项目可以做到零配置, 只是 libopencm3 的版本会旧一点. 这里说的是仅使用 VSCode 创建C/CPP项目时的配置. VSCode 有代码提示, 定位来源和各种快捷键, 更适合日常编码工作.

说明

因为 PlatformIO 的 platform:st-stm32 自带 libopencm3, 如果用 PlatformIO 创建 libopencm3 项目可以做到零配置, 只是 libopencm3 的版本会旧一点. 这里说的是仅使用 VSCode 创建C/CPP项目时的配置. VSCode 有代码提示, 定位来源和各种快捷键, 更适合日常编码工作.

前提条件

参考如何linux环境下配置环境变量过程图解  ,自行百度先将 GNU Arm Embedded Toolchain 和 st-flash 工具准备好

创建项目

导出模板项目

git clone --recurse-submodules https://github.com/libopencm3/libopencm3-template.git
或者
git clone --recurse-submodules https://gitee.com/iosetting/libopencm3-template.git

VSCode 创建项目

用 VSCode 的 Open Folder 打开. 需要修改一下 my-project/Makefile 中的配置, 将 DEVICE 修改为实际使用的MCU型号

DEVICE=stm32f103c8t6

配置C/CPP环境

快捷键Ctrl+Shift+P, 在打开的对话框中, 输入/找到 C/C++: Edit Configurations (JSON), 用JSON进行配置

配置内容

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/libopencm3/include"
            ],
            "defines": [
                "STM32F1"
            ],
            "compilerPath": "/opt/gcc-arm/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc",
            "cStandard": "gnu11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "gcc-arm"
        }
    ],
    "version": 4
}
  1. compilerPath 根据自己的工具链路径进行修改
  2. defines 下的 STM32F1 与编译无关(编译使用的是DEVICE和链接库), 不设置也能正确编译, 设置这个是为了 VSCode 能正确定位变量和函数声明

配置编译任务

快捷键Ctrl+Shift+P, 在打开的对话框中, 输入/找到 Tasks: Configure Task, 用others模板创建

配置内容, 其中TARGETS=stm32/f1根据实际的MCU型号修改

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build libopencm3",
            "type": "shell",
            "command": "PREFIX=/opt/gcc-arm/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi- TARGETS=stm32/f1 make -C libopencm3",
            "problemMatcher": []
        },
        {
            "label": "build clean libopencm3",
            "type": "shell",
            "command": "PREFIX=/opt/gcc-arm/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi- make -C libopencm3 clean",
            "problemMatcher": []
        },
        {
            "label": "build",
            "type": "shell",
            "command": "PREFIX=/opt/gcc-arm/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi- make -C my-project",
            "problemMatcher": []
        },
        {
            "label": "build clean",
            "type": "shell",
            "command": "PREFIX=/opt/gcc-arm/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi- make -C my-project clean",
            "problemMatcher": []
        },
        {
            "label": "build download",
            "type": "shell",
            "command": "st-flash --reset write my-project/awesomesauce.bin 0x8000000",
            "problemMatcher": []
        }
    ]
}

使用时, 通过Shift + Alt + F10调出菜单并选中执行.

先执行一次 build libopencm3 , 生成 libopencm3 的链接库之后, 编译项目就只需要执行 build 了.

返回顶部
顶部