VScode配置C环境和导入keil工程

  • Embed
  • 1,655 clicked

1. 安装VSCode

2. 安装Window Gcc环境

安装完成后,在cmd控制面板输入指令:

# 对安装的gcc查看版本,执行成功表示安装成功。
$ gcc -v

3. VSCode安装C/C++

3.1. 安装C/C++

3.2. 在.vscode文件夹中创建三个.json文件

vscode中,按ctrl+shift+p,然后输入C++,点击 C++编辑配置,自动生成'c_cpp_properties.json'文件内容。通常不需要修改,如果需要修改如下内容:

  • 'c_cpp_properties.json',其中需要根据实际情况修改configurations.includePath, configurations.browse.path
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceRoot}",
                "D:/VSCODE/EXE/mingw64/include/**",
                "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "__GNUC__=6",
                "__cdecl=__attribute__((__cdecl__))"
            ],
            "intelliSenseMode": "msvc-x64",
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": "",
                "path": [
                    "${workspaceRoot}",
                    "D:/VSCODE/EXE/mingw64/include/**",
                    "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                    "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                    "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                    "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                    "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                    "D:/VSCODE/EXE/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
                ]
            }
        }
    ],
    "version": 4
}

3.3. launch.json文件内容

  • 这里建议debug自动生产
    {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Launch",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "cmd",
            "preLaunchTask": "echo",
            "args": [
                "/C",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "&",
                "echo.",
                "&",
                "pause"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole":true
        },
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\VSCODE\\EXE\\bin\\gdb.exe",// 自己电脑的gdb
            "preLaunchTask": "echo",//这里和task.json的label相对应
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
    }

3.4. tasks.json文件内容

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "gcc",
            "args": [
                "-g", 
                "${file}", 
                "-o", 
                "${fileBasenameNoExtension}.exe",
                "-fexec-charset=GBK"//解决中文乱码
            ]
        }
    ],
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared", 
        "showReuseMessage": true,
        "clear": false
    }
}

3.5. 写一个简单的"Hello,World!"代码

#include "stdio.h"

void main(){
    printf("Hello World!\n");
}

3.6. 运行代码(F5)检查C环境是否配置成功,打印出"Hello,World!"即成功!(可以将.vscode中的三个文件保存起来,以后创建.c文件时直接复制即可)


4. VScode导入keil工程

VScode中不仅能打开keil工程,还能够进行修改和编译. 若项目根目录和导入项目的文件位置 不在同一目录下, 需要选择NO并手动选择项目工作区域。 建议项目路径为英文,否则在st-link中会报错文件错误。

4.1. 安装Embedded IDE插件

4.2. 点击设置工具链

4.3. 选择’Keil MDK’

4.4. 找到keil安装路径,选择’TOOLS.INI’文件

4.5. 导入keil工程,选择MDK

4.6. 编译工程

4.7. 在烧录和芯片参数配置

  • 修改宏定义/引用头文件目录/修改烧录方式

  • 芯片支持安装pack包,支持芯片

根据具体情况选择烧录方式,例如更换j-link/st-link等

Error: Old ST-LINK firmware version. Upgrade ST-LINK firmware
Error: Old ST-LINK firmware!Please upgrade it.

4.8. 更新ST-LINK 固件版本

点击下载,可直接使用ST-LNK将程序烧录到开发板

选择CPU架构和加密算法

4.9. 到此,VScode配置C环境和导入keil工程完毕!

4.10. 导出工程模板

右键你需要导出作为模板的项目,选择导出EIDE模板

压缩导出需要一定的时间,导出完成会在右下角有提示,并且很快消失

导出完成之后你就可以到你这个工程的根目录下找到一个后缀为.EPT的文件,将他放到你的模板库里方便下次建立工程的时候使用。

下次使用的时候只需要从本地项目模板内新建项目,填写名称,选择项目路径就可以直接编写程序进行烧录了。

5. 参考文档

发表评论

邮箱地址不会被公开。 必填项已用*标注