专业的编程技术博客社区

网站首页 > 博客文章 正文

vscode使用vs2022 msvc编译器

baijin 2024-12-23 10:25:25 博客文章 17 ℃ 0 评论

vscode常用的编译器一般是mingw,不过有时可能也用到msvc编译器,需要注意的是直接打开vscode是不行的,必须在Visual Studio开发者命令窗口中启动vscode,下面简单介绍下操作。

打开Visual Studio2022开发者命令

开始菜单中找到Developer Command Prompt for VS 2022

定位代码文件夹

cd D:\code3\codex\hi2

这里如果cd之后提示符还在c盘,再输入d:回车即可

输入code+空格+点号再回车,即可启动vscode并打开代码所在文件夹

.vscode下面的json内容

c_cpp_properties.json

{
  "configurations": [
    {
      "name": "windows-msvc-x64",
      "includePath": [
        "${workspaceFolder}/**"
      ],
      "compilerPath": "D:/vs2022/Enterprise/VC/Tools/MSVC/14.39.33519/bin/Hostx64/x64/cl.exe",
      "cStandard": "${default}",
      "cppStandard": "${default}",
      "intelliSenseMode": "windows-msvc-x64",
      "compilerArgs": [
        ""
      ]
    }
  ],
  "version": 4
}

其中compilerPath需要改成电脑里面cl.exe的路径

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C/C++ Runner: Debug Session",
      "type": "cppdbg",
      "request": "launch",
      "args": [],
      "stopAtEntry": false,
      "externalConsole": true,
      "cwd": "${workspaceFolder}",
      "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe",
      "MIMode": "gdb",
      "miDebuggerPath": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ]
    }
  ]
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "msvc build",
            "type": "shell",
            "command": "cl.exe",
            "args": [
                "/EHsc",
                "/Zi",
                "/Fe:",
                "helloworld.exe",
                "helloworld.cpp"
            ],
            "group": "build",
            "presentation": {
                "reveal": "always"
            },
            "problemMatcher": "$msCompile"
        },
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe 生成活动文件",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}",
                ""
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ]
}

设置好后就可以运行代码了,总的来说比配置mingw要麻烦些

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表