Inhaltsverzeichnis

C/C++ in Visual Studio Code

Windows

launch.json & tasks.json

.vscode/launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C (clang)",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "miDebuggerPath": "lldb",
            "setupCommands": [],
            "preLaunchTask": "C (clang)"
        },
        {
            "name": "C++ (clang++)",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "miDebuggerPath": "lldb",
            "setupCommands": [],
            "preLaunchTask": "C++ (clang++)"
        },
        {
            "name": "C/C++ (cl)",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "console": "integratedTerminal",
            "preLaunchTask": "C/C++ (cl)"
        }
    ]
}
.vscode/tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "C (clang)",
            "type": "cppbuild",
            "command": "clang",
            "args": [
                "-fcolor-diagnostics",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "C (clang)"
        },
        {
            "label": "C++ (clang++)",
            "type": "cppbuild",
            "command": "clang++",
            "args": [
                "-fcolor-diagnostics",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "C++ (clang++)"
        },
        {
            "label": "C/C++ (cl)",
            "type": "shell",
            "command": "${workspaceFolder}/.vscode/build.ps1",
            "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe:",
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$msCompile"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "C/C++ (cl)"
        }
    ]
}

LLVM/Clang

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

mehr infos bezüglich msys2: https://msys2.org/

für folgende PowerShell Kommandos werden Admin Rechte benötigt

# install msys2
winget install -e --id msys2.msys2
 
# open clang environment of msys2 (new window)
C:/msys64/msys2_shell.cmd -clang64
pacman -Syu
 
# confirm with Y (window will close) and repeat:
C:/msys64/msys2_shell.cmd -clang64
pacman -Syu
 
# install clang toolchain (just choose all)
pacman -S --needed base-devel mingw-w64-clang-x86_64-toolchain
 
# when up to date, close window/exit with ctrl+d
 
# update windows path
$msys2Path = "C:\msys64\clang64\bin"
$envTargetMachine = [System.EnvironmentVariableTarget]::Machine
$envPath = [System.Environment]::GetEnvironmentVariable("Path", $envTargetMachine)
$envPathList = [System.Collections.Generic.List[string]]::new($envPath.Trim(";").Split(";"))
$envPathList.Add($msys2Path)
$envPath = [string]::Join(";", $envPathList) + ";"
[System.Environment]::SetEnvironmentVariable("Path", $envPath, $envTargetMachine)
.vscode/c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:/msys64/clang64/bin/clang++.exe",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "windows-clang-x64"
        }
    ],
    "version": 4
}

GCC

MSVC

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

.vscode/c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.22000.0",
            "compilerPath": "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.32.31326/bin/Hostx64/x64/cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}
.vscode/build.ps1
$ErrorActionPreference = "Stop"
$VSPath = "C:/Program Files/Microsoft Visual Studio/2022/Community"
Import-Module "$VSPath/Common7/Tools/Microsoft.VisualStudio.DevShell.dll"
Enter-VsDevShell -VsInstallPath $VSPath
$command = "cl"
foreach ($arg in $args) { $command += " `"$arg`"" }
Write-Host "Build Command: `n$command`n"
Invoke-Expression $command