Visual Studio Code 中配置MSVC

Configure VS Code for Microsoft C++

c_cpp_properties.json

{
  "configurations": [
    {
      "name": "Win32",
      "includePath": ["${workspaceFolder}/**"],
      "defines": ["_DEBUG", "UNICODE", "_UNICODE"],
      "windowsSdkVersion": "10.0.18362.0",
      "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.43.34808/bin/Hostx64/x64/cl.exe",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "msvc-x64"
    }
  ],
  "version": 4
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "C/C++: cl.exe build and debug active file",
        "type": "cppvsdbg",
        "request": "launch",
        "program": "${workspaceFolder}/build/${fileBasenameNoExtension}.exe",
        "args": [],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": false,
        "preLaunchTask": "C/C++: cl.exe build active file"
      }
    ]
  }

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "Create build directory",
      "command": "cmd.exe",
      "args": [
        "/c",
        "if not exist ${workspaceFolder}\\build mkdir ${workspaceFolder}\\build"
      ],
      "problemMatcher": [],
      "group": {
        "kind": "build",
        "isDefault": false
      }
    },
    {
      "type": "shell",
      "label": "C/C++: cl.exe build active file",
      "command": "cl.exe",
      "args": [
        "/Zi",
        "/EHsc",
        "/Fe:",
        "${workspaceFolder}/build/${fileBasenameNoExtension}.exe",
        "src/*.cpp"
      ],
      "problemMatcher": ["$msCompile"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "dependsOn": "Create build directory",
      "detail": "Task generated by Debugger."
    }
  ]
}

test.cpp

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};

    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

打开工程的时候需要从Developer Command Prompt for VS中启动,否则无法加载对于的库。

进到工程根目录,执行"code ."

Visual Studio CodeVSCode)中配置includePath是为了让编辑器能够识别和自动完成C/C++的头文件。以下是配置includePath的基本步骤: 1. 安装C/C++扩展:首先确保在VSCode中安装了Microsoft的C/C++扩展,这个扩展提供了对C/C++语言的支持,包括智能感知(IntelliSense)功能。 2. 打开你的C/C++项目:在VSCode中打开你的项目文件夹。 3. 打开或创建`.vscode`文件夹:在项目根目录下找到或创建一个名为`.vscode`的文件夹。 4. 创建或编辑`c_cpp_properties.json`文件:在`.vscode`文件夹中,创建一个名为`c_cpp_properties.json`的文件,如果已经存在,则编辑这个文件。这个文件用于配置C/C++的编译器路径、include路径等。 5. 设置includePath:在`c_cpp_properties.json`文件中的`configurations`数组中,找到你使用的编译器配置部分,然后在`includePath`键中添加你的头文件路径。例如,如果你想包含标准库头文件路径和你的项目中特定的头文件目录,可以这样配置: ```json { "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**", "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/include", "C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "windowsSdkVersion": "10.0.19041.0", "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "${default}" } ], "version": 4 } ``` 6. 保存文件并重启VSCode:保存`c_cpp_properties.json`文件后,VSCode通常会自动重新加载配置,智能感知应该会立即生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值