Vscode編譯構建C++

linux

  1. 首先安裝好g++
  2. 編寫好文件main.cpp
  3. 編譯文件,需要構建tasks.json;用來編譯

我的比較簡單的

{
    // 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++",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "args": [
                "*.cpp",
                "-o",
                "a.out"
     
            ]
        }
    ]
}

詳細的

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true  // 默認生成任務,即按“Ctrl+Shift+B”快捷鍵會自動運行的任務
            }, 
            "linux": {
                "command": "g++",
                "args": [
                    "-g",           // 生成調試信息
                    "-Wall",        // 開啓所有警告
                    "*.cpp",        // 編譯所cpp文件
                    "-lpthread",    // 鏈接pthread庫(Linux系統必需) 
                    "--std=c++11",  // 使用C++11標準 
                    "-o",
                    "paralle_for_each.out" // 指定輸出文件名
                ]
            }, 
            "problemMatcher": {
                "owner": "cc",
                "fileLocation": [
                    "relative",
                    "${workspaceFolder}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            } 
            
        } 
    ]
}


4.點到蜘蛛那裏,運行,然後會讓你構建一個launch.json;修改其中一行
"program": "${workspaceFolder}/a.out",

我的

{
    // 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}/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}/paralle_for_each.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
  1. 接着終端窗口就會彈出運行結果
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章