https://embaud-ot.hateblo.jp/entry/20210124_VScode_WSL2_CppCompile

Windows10で、VSCodeを使ったC言語の開発環境を作りました。MinGWを使わず、WSL2のubuntu内にあるコンパイラを使う環境を今回作りました。

手順

 wsl --set-default-version 2
sudo apt update
sudo apt install build-essential
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/**",
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}
{
    // See <https://go.microsoft.com/fwlink/?LinkId=733558>
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "options": {
                "shell": {
                    "executable": "C:\\\\Windows\\\\System32\\\\wsl.exe"
                }
            },
            "command": "g++",
            "args": [
                "-std=gnu++1y",
                "-g",
                "-O0",
                "-I/opt/boost/gcc/include",
                "-L/opt/boost/gcc/lib",
                "-o",
                "`wslpath",
                "'${workspaceFolder}\\\\output.exe'`",
                "`wslpath",
                "'${file}'`"
            ],
            "group": "build",
            "problemMatcher": []
        },
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: /usr/bin/gcc"
        }
    ]
}
#include <stdio.h>

int main(int argc, char **argv){
    printf("Hello World!\\n");
    return 0;
}
  1. Ctl+Shift + B でビルド開始
> Executing task: C/C++: gcc build active file <

Starting build...
Build finished successfully.

Terminal will be reused by tasks, press any key to close it.
  1. Ctl + Shift + @ でターミナルを開き、ビルドデータの確認をする
  2. ビルドデータを実行する
{
    // 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": "gcc - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}