首页 > 编程语言 > 详细

在Windows平台搭建msvc(cl.exe) + vscode环境调试C/C++

时间:2021-06-29 22:32:09      阅读:15      评论:0      收藏:0      [点我收藏+]

前期准备&基础知识

参考链接

  1. https://blog.csdn.net/heilone6688/article/details/91050508
  2. https://blog.csdn.net/faithzzf/article/details/52328353

软件安装

  1. vscode
  2. vs
  3. vscode 中的 c/c++ 插件

环境变量

  1. path:编译器cl.exe所在路径
"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/um/x64/kernel32.lib"
  1. INCLUDE: 头文件路径
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/ucrt"

"C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/include"
  1. LIB: lib文件路径:
"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/um/x64/kernel32.lib"

"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/ucrt/x64/libucrt.lib"
              
"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/um/x64/Uuid.lib"

cl.exe的编译语法

cl.exe /EHsc /Zi /Fe: 源文件.cpp 要生成的可执行文件名.exe /I 包含的头文件路径 /link 要连接的库文件
其中,/I是大写的i

具体设置

1. 在工程目录下新建文件夹.vscode

在.vscode文件夹里新建四个json文件:

  1. c_cpp_properties.json
  2. launch.json
  3. settings.json
  4. tasks.json

结构如下:

  • .vscode
    • c_cpp_properties.json
    • launch.json
    • settings.json
    • tasks.json

2. 修改c_cpp_properties.json的内容

{
  "configurations": [
    {
      "name": "Win32", //自己起的名字
      "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE"
      ],
      "windowsSdkVersion": "10.0.17763.0",
      "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe", //编译器的路径(可以在VScode中的file-Preference-Settings-Extensions-Cpp compiler Path中设置)
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "msvc-x64",
      "compilerArgs": []   
    }
  ],
  "version": 4
}

3. 修改launch.json的内容

{
  "version": "2.0.0",
  "configurations": [
    {
      "name": "cl.exe build and debug active file", //自己起的名字
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}.exe", //目标可执行文件路径
      "args": [],
      "cwd": "${workspaceFolder}",
      "environment": [],
      "preLaunchTask": "cl.exe build active file", //之前运行的shell名称,即tasks.json中的label字段
      "console": "externalTerminal"
    }
  ]
}

4. 修改settings.json的内容

{

    "C_Cpp.default.compilerPath": "C://Program Files (x86)//Microsoft Visual Studio//2017//BuildTools//VC//Tools//MSVC//14.16.27023//bin//Hostx64//x86//cl.exe",
    "C_Cpp_Runner.cppCompilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/bin/Hostx64/x64/cl.exe", 
    "http.proxyAuthorization": null,
    "[c]": {
        "files.encoding": "gbk" //使用gbk编码
    },
    "[cpp]": {
        "files.encoding": "gbk"
    },
    "C_Cpp_Runner.cCompilerPath": "",
}

5. 修改tasks.json的内容

{
    "tasks": [
        {
            "label": "cl.exe build active file", //自己起的标签,在vscode中显示,注意,这个要和launch.json中的"preLaunchTask"里一样
            "type": "shell",
            "command": "cl.exe",
            "args": [  //命令行参数
                "/EHsc",
                "/Zi",
                "/Fe:",
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "${fileDirname}/${fileBasenameNoExtension}.cpp",
                "/I",
                "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.28.29910/include",
                "/I",
                "C:/Program Files (x86)/Windows Kits/10/Include/10.0.17763.0/ucrt",
                "/link",
                "C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/um/x64/kernel32.lib",
                "C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/ucrt/x64/libucrt.lib",
                "C:/Program Files (x86)/Windows Kits/10/Lib/10.0.17763.0/um/x64/Uuid.lib"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "reveal":"always"
            },
            "problemMatcher": "$msCompile",
            "options": {
                "cwd": "${fileDirname}"
            },
            "detail": "编译器: cl.exe"
        }
    ],
    "version": "2.0.0"
  }

6. 大功告成,在vscode中新建源文件按F5运行吧

在Windows平台搭建msvc(cl.exe) + vscode环境调试C/C++

原文:https://www.cnblogs.com/Dre2mspace/p/14951718.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!