VS Code 在Linux下IDE開發C++的HelloWorld

用Visual Studio Code 在Linux(Ubuntu)下構造c++ 的集成開發環境,編輯,編譯和調試運行一個簡單程序HelloWorld。

想達到上面目標,搜索到以下文章,學習驗證而成本文日記。

鏈接是:https://code.visualstudio.com/docs/cpp/config-linux

前期準備

運行環境是ubuntu16.0,先安裝好Visual Studio Code(VS Code)

安裝好VS Code 的C++ 擴展,可以在VS code裏Ctrl+Shift+X,然後輸入C++

確保gcc 安裝好了,檢查方法是:

gcc --version

如果沒有,先更新系統

sudo apt-get update

然後如下安裝gcc 和 gdb

sudo apt-get install build-essential gdb

我的ubuntu 16 這樣後,gcc版本是5.8,有點低。

建立helloworld

在終端建立一個helloworld目錄,然後啓動VScode,如下:

mkdir helloworld
cd helloworld
code .

本實驗完成後,將在工作空間目錄建立3個文件

  • tasks.json (編譯鏈接設置)
  • launch.json (調式設置)
  • c_cpp_properties.json (c++屬性設置)

在文件資源管理器標題欄中,選擇“新建文件”並命名文件helloworld.cpp

編輯或粘貼下面代碼:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    // Create an empty vector 
    vector<int> vect;  
     
    vect.push_back(10); 
    vect.push_back(20); 
    vect.push_back(30);
	
    for (int i = 0; i <vect.size(); i++)
    {
        cout << vect[i] << endl;
    }
    cout << endl;
}

原文的代碼要求gcc版本比較高,我修改如上,你也可以改成更簡單的代碼。

ctrl + S 保存文件

VScode 的文件瀏覽器裏可以看到這個文件:

編譯helloworld

這一步將建立tasks.json 文件。

主菜單裏 Terminal > Configure Default Build Task

一個下拉菜單裏讓你選擇編輯器

根據你的情況選擇編譯工具,這就是上文要準備好的gcc, 上圖我是複製的,我自己的界面不一樣,當然類似。

這樣就建立了tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "g++ build active file",
      "command": "/usr/bin/g++",
      "args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
      "options": {
        "cwd": "/usr/bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

更多task.json 變量請參考 variables reference.

在這裏${file}指當前活動文件,當打開文件helloworld.cpp,就是helloworld.cpp。

編譯鏈接

回到helloworld.cpp, 這個很重要,這時 ${file} =helloworld.cpp ,

按Ctrl+Shift+B 或者主菜單>Terminal>Run Build Task

編譯鏈接就按task.json設置編譯鏈接。

有錯就報錯,沒有報錯,應該如下類似顯示:

> Executing task: /usr/bin/g++ -g /home/liwenz/helloworld/helloworld.cpp -o /home/liwenz/helloworld/helloworld <


Terminal will be reused by tasks, press any key to close it.

我們可以看到參數的替換 

/home/liwenz/helloworld/helloworld.cpp =>  ${file}

/home/liwenz/helloworld/helloworld=>${fileDirname}/${fileBasenameNoExtension}

走完第一個helloworld流程後,再做這個helloworld時,我沒注意,編譯時打開tasks.json , 結果總報錯,我當時還不會看錯誤,錯誤指示如下:

Executing task: /usr/bin/g++ -g /home/liwenz/helloworld/.vscode/tasks.json -o /home/liwenz/helloworld/.vscode/tasks <

/usr/bin/ld:/home/liwenz/helloworld/.vscode/tasks.json: file format not recognized; treating as linker script /usr/bin/ld:/home/liwenz/helloworld/.vscode/tasks.json:1: syntax error collect2: error: ld returned 1 exit status The terminal process terminated with exit code: 1

現在回過頭來看,看到 task 的內容,就知道怎麼錯了。

修改tasks.json

上面的task.json 可以根據我們需要修改。

如果要編譯多個.cpp 文件,可以修改

"${workspaceFolder}/*.cpp" 替換 ${file}

輸出則固定名字,比如hello 替換"${fileDirname}/${fileBasenameNoExtension}"

這樣就不必一定要回到helloworld.cpp 然後才能編譯鏈接。

Debug helloworld.cpp

這一步我們創建 launch.json

在主菜單 Run > Add Configuration... 然後選擇 C++ (GDB/LLDB)

在下拉菜單裏選擇 g++ build and debug active file.

這樣就形成了一個launch.json 文件

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g++ build and debug active file",
      "type": "cppdbg",
      "request": "launch",
      "program": "${fileDirname}/${fileBasenameNoExtension}",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "g++ build active file",
      "miDebuggerPath": "/usr/bin/gdb"
    }
  ]
}

 缺省情況下, stopAtEntry 設置成 false, 這樣不會有任何斷點,修改爲true, 則Debug時會停在 main 函數開始處。

開始調試

回到打開helloworld.cpp 的狀態,F5 或者菜單 Run > Start Debugging 就開始調試了。同樣沒有修改這個launch.json ,必須回到helloworld.cpp 因爲裏面包含了${file}等。

可以預先設置斷點,或者至少stopAtEntry修改爲true。設置斷點的方法可以鼠標在行前直接點直接改變,也可以光標到一行,然後F9 或者菜單 Run > Toggle Breakpoint,乒乓改變斷點狀態。還有Run>Add New Breakpoint方法,先不學。

停到斷點後,出現下面Debug 控制條,意義鼠標放在上面有提示,分別是暫停(下圖中爲灰),繼續運行,單步,單步內進,單步外退,重新開始,停止。

可以Add Watch ,也可以看到各變量的值

C/C++ configurations(配置)

如果要更進一步配置c/c++,則可以用 c_cpp_properties.json 文件。

Ctrl+Shift+P  然後輸入c/c++ 

這裏只是簡單提一下。

配置的重用

把配置文件複製到新工程的 .vscode 目錄下即可,然後根據情況修改這幾個json 文件。

介紹到此,可以看看我學習的鏈接。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章