c++

VS code 配置现代C++ 环境

VS code configures a modern C++23 environment

Posted by hwdong on February 26, 2025

https://code.visualstudio.com/docs/cpp/config-msvc

Configure VS Code for Microsoft C++

  1. Install Visual Studio Code.
  2. 安装 VS Code 的 C/C++ 扩展 Install the C/C++ extension for VS Code. You can install the C/C++ extension by searching for ‘c++’ in the Extensions view (Ctrl+Shift+X).
  3. 安装 Microsoft Visual C++ (MSVC) 编译器工具集(Install the Microsoft Visual C++ (MSVC) compiler toolset.) 下载安装Visual Studio 2022( Build Tools for Visual Studio 2022)
  4. To use MSVC from a command line or VS Code, you must run from a Developer Command Prompt for Visual Studio.

tasks.json for msvc:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/std:c++20",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Using GCC with MinGW

  1. Install Visual Studio Code.
  2. Install the C/C++ extension for VS Code. You can install the C/C++ extension by searching for ‘c++’ in the Extensions view (Ctrl+Shift+X).
  3. Installing the MinGW-w64 toolchain or Installed offline winlibs-x86_64-posix-seh-gcc-12.2.0-llvm-15.0.7-mingw-w64ucrt-10.0.0-r4 from winlibs.
  4. In this terminal, install the MinGW-w64 toolchain by running the following command:
    pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
    
  5. Add the path of your MinGW-w64 bin folder to the Windows PATH environment variable

tasks.json for g++:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe build active file",
            "command": "C:\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-std=c++23",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

Check if C++23 is supported

#include <iostream>
#include <ranges>
#include <vector>

int main() {
    // 创建一个从 0 到 9 的范围
    auto rng = std::views::iota(0, 10);

    // 使用 std::ranges::to 转换为 std::vector
    std::vector<int> vec = rng | std::ranges::to<std::vector>();

    // 输出转换后的结果
    for (const auto& val : vec) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

解释

  • std::ranges::to 是 C++23 引入的,它可以把一个范围(range)转换为容器,如 std::vector、std::list 等。
  • C++20 不支持 std::ranges::to,因此在 C++20 中你不能直接使用这个功能来将范围转换为容器。
  • 这个程序运行时将输出:
    0 1 2 3 4 5 6 7 8 9
    

    这种方式简化了从范围到容器的转换,避免了手动插入元素的繁琐过程。

compiling your code with GCC/Clang:

g++ -std=c++23 main.cpp -o main

compiling your code with msvc

cl /std:c++23 main.cpp

支付宝打赏 微信打赏

您的打赏是对我最大的鼓励!