Ubuntu中用VScode編譯除錯C\C++

在下摸魚怪發表於2020-11-08

一、圖形安裝VScode

  1. 找到 Ubuntu 軟體,搜尋 vscode:
    Ubuntu中用VScode編譯除錯C\C++
  2. 點選進入,然後直接安裝
    Ubuntu中用VScode編譯除錯C\C++

二、環境搭建並測試

  1. 安裝 C/C++外掛
    Ubuntu中用VScode編譯除錯C\C++

  2. Ctrl+O 選擇一個資料夾(已建立好的)
    Ubuntu中用VScode編譯除錯C\C++

  3. 環境選擇
    Ubuntu中用VScode編譯除錯C\C++

    然後在選擇 g++ 什麼什麼

  4. 修改生成的 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": "${workspaceFolder}/${fileBasenameNoExtension}",
                "args": [],
                "stopAtEntry": false,
                "cwd": "${workspaceFolder}",
                "environment": [],
                "externalConsole": true,
                "MIMode": "gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            }
        ]
    }
    
  5. 修改生成的 task.json 檔案

    {
    	"version": "2.0.0",
    	"tasks": [
    		{
    			"label": "build",
    			"type": "shell",
    			"command": "g++",
    			"args": [
    				"-g",
    				"${file}",
    				"-std=c++11",
    				"-o",
    				"${fileBasenameNoExtension}"
    			]
    		}
    	]
    }
    
  6. 然後在 hello.cpp 中設定斷點,並執行除錯
    Ubuntu中用VScode編譯除錯C\C++

  7. 開始除錯
    Ubuntu中用VScode編譯除錯C\C++

三、參考?

相關文章