Ubuntu 16.04下Visual StudioコードコンパイルデバッグC++

4088 ワード

左側のデバッグボタン(Ctrl+Shift+D)をクリックし、Add configurationを選択し、C+(GDB/LDB)を選択して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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "enter program name, for example ${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

変更後:
{
    // 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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

次に、ショートカットキーCtrl+Shift+PまたはF 1を使用してコマンドラインを開き、次のように入力します.
Tasks: Run task

次の順に選択します.
No task to run found. configure tasks...
Create tasks.json file from template
Others

taskを生成する.jsonファイルのデフォルトは次のとおりです.
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello"
        }
    ]
}

変更後:
{
    // 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",
            "command": "g++",
            "args": ["-g", "${file}", "-std=c++11", "-o", "${fileBasenameNoExtension}.out"]
        }
    ]
}

最後に、ショートカットキーCtrl+Shift+PまたはF 1を使用してコマンドラインを開き、次のように入力します.
Tasks: Run Task
bulid
Continue without scanning the task output

または、ショートカットキーCtrl+Shift+Bを使用して、次のように入力します.
bulid
Continue without scanning the task output

これにより、outという接尾辞を付けた実行可能ファイルが生成され、Terminalで実行したり、VS CodeでショートカットキーF 5を押してデバッグしたりすることができます.