博客轉載:https://blog.csdn.net/weixin_43374723/article/details/84064644
Visual studio code是微軟發布的一個運行於 Mac OS X、Windows和 Linux 之上的,針對於編寫現代 Web 和雲應用的跨平台源代碼編輯器。
1. Vscode安裝
第一種方式是從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
2. Vscode環境配置
2.1 安裝c/c++插件
首先通過左邊欄的Extension欄目安裝C++插件,操作如下圖:
2.2 建立工程
由於VScode是以文件夾的形式管理工程的,因此我們首先新建一個文件夾,我這里取名叫hello
。
然后通過VScode打開此文件夾:
新建main.cpp文件並輸入程序:
2.3 更改配置文件(launch.json)
點擊左側的Debug按鈕,選擇添加配置(Add configuration),然后選擇C++(GDB/LLDB),將自動生成launch.json文件,具體操作如下:
將默認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++開發與調試了。