1. 程式人生 > 程式設計 >在Ubuntu中安裝VSCode並配置C/C++開發環境的方法步驟

在Ubuntu中安裝VSCode並配置C/C++開發環境的方法步驟

第一步,開啟Ubuntu Software下載VSCode。(so easy)

官網地址:https://code.visualstudio.com/docs/?dv=linux64_deb

然後使用 sudo dpkg -i xxx.deb解壓即可

第二步,如果你的電腦中沒有gcc,g++,gdb的話,可以通過以下程式碼獲得。

sudo apt-get update //訪問源列表,並讀取軟體列表
sudo apt-get install gcc
sudo apt-get install g++
sudo apt-get install gdb

第三步,開啟VSCode,安裝三個外掛
1.C/C++

2.Code Runner
3.Run in Terminal

第四步,新建一個資料夾,用VSCode開啟它,在那個資料夾裡新建一個.vscode資料夾,並在這個資料夾裡建立一個launch.json 和 一個tasks.json

兩個資料夾裡的內容是這樣的
launch.json

{
  "version": "0.2.0","configurations": [
    {
      "name": "C/C++","type": "cppdbg","request": "launch","program": "${fileDirname}/${fileBasenameNoExtension}","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","preLaunchTask": "compile","setupCommands": [
        {
          "description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true
        }
      ]
    }
  ]
}

tasks.json

{
  "version": "2.0.0","tasks": [{
      "label": "compile","command": "g++","args": [
        "-g","${file}","-o","${fileDirname}/${fileBasenameNoExtension}"
      ],"problemMatcher": {
        "owner": "cpp","fileLocation": [
          "relative","${workspaceRoot}"
        ],"pattern": {
          "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$","file": 1,"line": 2,"column": 3,"severity": 4,"message": 5
        }
      },"group": {
        "kind": "build","isDefault": true
      }
    }
  ]
}

這裡預設寫的是C++,如果要寫C的話,把command由 g++ 改為 gcc 即可。

最後講下編譯執行。~~~~

編譯執行的話其實我很推薦用Terminal。

Ubuntu中使用Terminal編譯執行C/C++程式

編譯 : g++ -o 想要得到的可執行檔案的名字 你想要編譯的檔案的名字

執行 : ./可執行檔名

例如:我有一個 Hello.cpp , 我想生成一個名為 Hello 的可執行檔案

首先 : g++ -o Hello Hello.cpp //編譯生成執行檔案
./Hello //執行可執行檔案

到此這篇關於在Ubuntu中安裝VSCode並配置C/C++開發環境的方法步驟的文章就介紹到這了,更多相關Ubuntu安裝VSCode配置C/C++開發環境內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!