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

博客轉載:http://www.javashuo.com/article/p-kdqtlcgo-kz.htmlc++

Visual studio code是微軟發佈的一個運行於 Mac OS X、Windows和 Linux 之上的,針對於編寫現代 Web 和雲應用的跨平臺源代碼編輯器。web

1. Vscode安裝
第一種方式是從VScode官網下載.deb文件,而後雙擊該文件會打開軟件中心進行安裝。
shell

第二種方式是經過Terminal進行安裝,首先輸入下面三條語句安裝umakejson

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

而後經過umake來安裝VScode:ubuntu

umake web visual-studio-code

2. Vscode環境配置

2.1 安裝c/c++插件

首先經過左邊欄的Extension欄目安裝C++插件,操做以下圖:visual-studio-code

2.2 創建工程

因爲VScode是以文件夾的形式管理工程的,所以咱們首先新建一個文件夾,我這裏取名叫hello編輯器

而後經過VScode打開此文件夾:visual-studio

新建main.cpp文件並輸入程序:ui

2.3 更改配置文件(launch.json)

點擊左側的Debug按鈕,選擇添加配置(Add configuration),而後選擇C++(GDB/LLDB),將自動生成launch.json文件,具體操做以下:spa

將默認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.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"]
        }
     ]
}

完整的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"]
        }
     ]
}

2.5 簡單斷點調試

通過上述配置以後就能夠對咱們寫的程序進行簡單的配置。在進行下面的操做前,咱們應當保證launch.json和tasks.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++開發與調試了。

相關文章
相關標籤/搜索