VS Code C++ 安装和环境配置(MSVC和MinGW)

准备工作

  • 从官网下载安装 VS Code: https://code.visualstudio.com/

  • VS Code 本身只是一个代码编辑器, 需要手动添加编译器、调试器

    • 这里选择MSVC(cl, cdb)和MinGW(gcc/g++, gdb)
    • MSVC可以通过VS安装器安装,MinGW从官网对应Host下载安装 MinGW: http://mingw-w64.org/doku.php/download(threads考虑跨平台选择posix标准)
    • 安装完以后将MinGW和MSVC的目录的bin文件夹添加到环境变量 Path 路径
      ( CMD 输入g++ -vcl验证是否安装完成)

C++项目创建

  • 手动创建一个工程目录, 例如MyProjects, 用来存放代码
  • 用VS Code打开该工程目录进行配置和coding
  • 推荐用这种方式来创建一个工程,方便识别
  • 在当前工程目录下创建一个.vscode文件夹

共存两种编译方式

  • 首先是创建编译任务配置文件tasks.jsonMSVC,MinGW文件夹,分别包含用cl和g++编译两个任务以及存放相关文件, 主要参数是args编译选项:
    tasks.json
{
    "tasks": [
        {
            "type": "shell",
            "label": "cl.exe build",
            "command": "cl.exe",
            "args": [
                //头文件路径
                "/I'D:\\Programs\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\include'",
                "/I'C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\shared'",
                "/I'C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\ucrt'",
                "/I'C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.18362.0\\um'",
                "${file}",
                "/Zi", //生成调试信息
                "/EHsc", //打开异常处理
                "/Fo:${fileDirname}\\MSVC\\${fileBasenameNoExtension}.obj", //目标文件
                "/Fe:${fileDirname}\\MSVC\\${fileBasenameNoExtension}.exe", //可执行文件
                "/link", //链接标志
                //库文件路径
                "/libpath:'D:\\Programs\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\lib\\x64'",
                "/libpath:'C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.18362.0\\ucrt\\x64'",
                "/libpath:'C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.18362.0\\um\\x64'"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal": "always"
            },
            "problemMatcher": "$msCompile"
        },
        {
            "type": "shell",
            "label": "g++.exe build",
            "command": "g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\MinGW\\${fileBasenameNoExtension}.exe",
                //头文件路径
                "-I D:\\Programs\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\x86_64-w64-mingw32\\include",
                //库文件路径
                "-L D:\\Programs\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\x86_64-w64-mingw32\\lib"
            ],
            "options": {
                "cwd": "D:\\Programs\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ],

    "version": "2.0.0"
}
  • 创建完成以后可以先单独测试编译, 置前当前需要编译的源文件(这个即为${file}), 然后点击Terminal => Run Task,按照标签分别测试两个compiler:
build.PNG
  • 编译没问题以后,下面就是配置启动和Debugger, 在.vscode中创建一个名为launch.json的文件, 主要是通过type来指定debugger和preLaunchTask将之前的两个编译tasks作为前置任务先执行(注意这里可以将externalConsole设置为false来创建一个console的子进程避免打开额外的进程窗口):

launch.json

{
    "version": "0.2.0",
    
    "configurations": [
        //cdb
        {
            "name": "cl.exe build and debug",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "MSVC\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false, //子进程console
            "preLaunchTask": "cl.exe build",
            "logging": {
                "moduleLoad": false
            },
        },
        //gdb
        {
            "name": "g++.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\MinGW\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++.exe build"
        }
    ]
}
  • 启动方法是选中锚点文件${file}, 例如这里的test.cpp, 点击左侧的Run图标,然后在左上角选择相应name的runner,cdb可以在下方的debug console查看信息,gdb直接在terminal查看:
    runner.png

(后记:对于多源文件和方便性可以使用CMake,参考我的这一篇VS Code C++ CMake工具编译,本文可以帮助复习一下编译工具相关的知识)

你可能感兴趣的:(VS Code C++ 安装和环境配置(MSVC和MinGW))