vscode 配置 msvc 编译器

910 阅读2分钟

前言:

参考:Configure Visual Studio Code for Microsoft C++
(4条消息) VsCode搭建Windows C++ (MSVC)开发环境_nisigesazi-CSDN博客_msdn vscode
cl.exe的编译选项docs.microsoft.com/zh-cn/cpp/b…
(4条消息) VsCode搭建Windows C++ (MSVC)开发环境_nisigesazi-CSDN博客_msdn vscode
(4条消息) VSCode配置C++环境(MSVC)_Harry的博客-CSDN博客_msvc
预定义变量:VScode tasks.json和launch.json的设置 - 知乎 (zhihu.com)
MinGW+VSCode:windows 10上使用vscode编译运行和调试C/C++ - 知乎 (zhihu.com)
VSCode配置C++环境的方法步骤(MSVC) | w3c笔记 (w3cschool.cn)
来自微软官网,警告级别及其他编译:/w、/W0、/W1、/W2、/W3、/W4、/w1、/w2、/w3、/w4、/Wall、/wd、/we、/wo、/Wv、/WX(警告级别) | Microsoft Docs

主要是这个思路:

  • tasks.json 是编译用的脚本文件(自己理解成脚本文件)
    • "任务",指可以编写多个任务
  • launch.json 是调试用的脚本(这个比较简单,一般只修改一处即可)
  • 这两个文件一般位于.vscode

示例

  • 生成二进制可执行文件
  • 指定了输出目录
  • 参与编译的源文件用通配符 *.cpp表示
  • f1 --> 键入tasks --> 自动补全 tasks.json tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",    
    // 进入 build 目录下,这样把编译的中间文件就放在这里了  
    "options": {
        "cwd": "${workspaceFolder}/build/"
    },
    "tasks": [
        {
            "label": "build release Win64",
            "type": "process",
            "command": "cl.exe",
            "args": [    
                // 警告级别设置   
                "/Wall",
                // "/EHsc",  
                // 开启增量编译,这样才能调试  
                "/Zi",  
                "/Fe:",  
                // 相当于编译依赖的说明 先是目标的说明, 然后是依赖项  
                // 上面的编译选项参考微软官方说明  
                // https://docs.microsoft.com/zh-cn/cpp/build/reference/compiler-options-listed-alphabetically?view=msvc-170
                "${workspaceFolder}/bin/hello.exe",
                // "${workspaceFolder}/src/vscode_msvc_test.cpp"    
                "${workspaceFolder}/src/*.cpp"  
                ],
            "group": "build",
            "problemMatcher": [
                "$msCompile"
            ]
        },  

        // 再添加个任务  
        // 删除 bin 和 build 文件夹下面的文件    
        // 注意! 要删除多个文件夹的话, 要用绝对路径, 不要引用变量 ${workspaceFolder}/          
        {
            "label":"clean",  
            "type":"shell"
            "command": "rm",  
            // 使用绝对路径,不能使用引用变量的方式
            "args": ["F:/Exercises/vscode_msvc_test/bin/*,", "F:/Exercises/vscode_msvc_test/build/*"],  
            "group": "none",
            "problemMatcher": []
        }

        // 再添加个任务,综合的 ReBuild  
        {
            "label":"ReBuild",  
            "dependsOrder": "sequence",
            "dependsOn":[
                "clean",  
                "build release Win64"                
            ]
        }          
    ]
}

launch.json

  • f5自动提示需要填写launch.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) 启动",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/hello.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "console": "externalTerminal"
        }
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win64",
            "includePath": [
                "${workspaceFolder}/**"
                 "${env:INCLUDE}",
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.17763.0",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64",
            "compilerPath": "E:\\Microsoft Visual Studio\\2017\\Professional\\VC\\Tools\\MSVC\\14.16.27023\\bin\\Hostx64\\x64\\cl.exe"
        }
    ],
    "version": 4
}