1. 程式人生 > >Ubuntu16.04下配置VScode的C/C++開發環境

Ubuntu16.04下配置VScode的C/C++開發環境

VScode環境搭建

1. Vscode安裝

Visual studio code是微軟釋出的一個運行於 Mac OS X、Windows和 Linux 之上的,針對於編寫現代 Web 和雲應用的跨平臺原始碼編輯器。
第一種方式是從

VScode官網下載.deb檔案,然後雙擊該檔案會開啟軟體中心進行安裝。

另一種方式是通過Terminal進行安裝,首先輸入下面三條語句安裝umake

sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make
sudo apt-get update
sudo apt-get install ubuntu-make

然後通過umake來安裝VScode:

umake web visual-studio-code

安裝完畢後即可開啟VScode,主介面如下:

2. Vscode環境配置

(1)安裝c/c++外掛

首先通過左邊欄的Extension欄目安裝C++外掛,操作如下圖:

(2)建立工程

由於VScode是以資料夾的形式管理工程的,因此我們首先新建一個資料夾,我這裡取名叫hello

然後通過VScode開啟此資料夾:

新建main.cpp檔案並輸入程式:

(3)更改配置檔案(launch.json)

點選左側的Debug按鈕,選擇新增配置(Add configuration),然後選擇C++(GDB/LLDB),將自動生成launch.json檔案,具體操作如下:

生成的預設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 } ] } ] }

注意:這裡需要將program項的內容改為除錯時執行的程式,將其改為main.out即可。具體更改如下:

            "program": "enter program name, for example ${workspaceFolder}/a.out",

改為

            "program": "${workspaceFolder}/main.out",

該語句指的是當前工作資料夾下的main.out檔案,更改完畢的launch.json檔案見附錄。

(4)新增構建(編譯、連結等)任務(tasks.json)

為了方便在VScode裡編譯C++程式碼,我們可以將類似g++ -g main.cpp等g++命令寫入VScode的任務系統。
首先,利用快捷鍵ctrl+shift+p開啟命令列,輸入Tasks: Run task,會出現如下提示:

No task to run found. configure tasks...

回車,然後依次選擇如下:

Create tasks.json file from template

Others Example to run an arbitrary external command.

生成預設的tasks.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"
        }
    ]
}

這裡的label為任務名,我們將”label"= "echo"改為”label"= "build"
由於我們的指令是g++,這裡將”command“=”echo Hello“改為”command“=”g++“
然後新增g++的引數args。如果我們的g++指令為:g++ -g main.cpp,這裡可以把引數設定為如下:

{
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": ["-g", "${file}"]
        }
      ]
}

如果我們想配置g++指令為:g++ -g main.cpp -std=c++11 -o main.out,則引數可設定為:

{
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": ["-g", "${file}", "-std=c++11", "-o", "${fileBasenameNoExtension}.out"]
        }
     ]
}

我們可以通過舉一反三來配置不同的g++指令。完整的tasks.json檔案可參考附錄。

(5)簡單斷點除錯

經過上述配置之後就可以對我們寫的程式進行簡單的配置。在進行下面的操作前,我們應當保證launch.jsontasks.json的正確性並且已經成功儲存。

使用快捷鍵ctrl+shift+p調出命令列,選擇執行我們的build任務,build成功後,點選開始除錯。具體操作如下:

值得注意的是,這裡如果每次更改了程式需要重新build,然後再進行除錯;如果直接進行除錯則執行的是上次build的結果。通過在launc.json作如下更改可以使得每次除錯之前會自動進行build

這裡在launch.json檔案中添加了”preLaunchTask“=”build",也就是新增一個launch之間的任務,任務名為build,這個build就是我們在tasks.json中設定的任務名。

3.總結及注意事項

本文對Ubuntu16.04系統下配置基於VScode的C/C++開發環境進行了簡單的介紹,主要步驟為:
1.安裝VScode,可以通過在官網下載和命令列的方式進行安裝。(順便提一下,在命令列安裝的過程中可能會讓你輸入a)
2.新建C/C++工程,VScode以資料夾為管理工程的方式,因此需要建立一個資料夾來儲存工程。
3.配置launch.json檔案,它是一個啟動配置檔案。需要進行修改地方的是指定執行的檔案,其次我們還可以在裡面新增build任務。
4.配置tasks.json檔案,這個檔案用來方便使用者自定義任務,我們可以通過這個檔案來新增g++/gcc或者是make命令,方便我們編譯程式。
5.上述四個流程完了之後我們就可以進行基礎的C/C++開發與除錯了。

4. 附錄

這裡給出一個較完整的配置檔案和任務檔案,筆者的系統的Ubuntu16.04 LTS,測試時間是2018/11/14。由於版本不同可能會有所變化,因此該配置僅供參考!

(1)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}.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
                }
            ]
        }
    ]
}

(2)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++",
            "args": ["-g", "${file}", "-std=c++11", "-o", "${fileBasenameNoExtension}.out"]
        }
     ]
}