相關文章
準備工作及環境說明
- ubuntu版本:17.10 x64
- vscode下載:https://code.visualstudio.com/
安裝vscode
- 打開終端(快捷鍵:Ctrl+Alt+T)
- 運行如下命令:
sudo dpkg -i vscode.deb
- 如果安裝失敗,則需要運行如下命令:
sudo apt-get install -f
之後再次安裝即可以成功,相關資料可參考官方文檔:
https://code.visualstudio.com/docs/setup/linux
C/C++ 運行
運行過程和用終端是一樣的,需要先使用g++/gcc對程序進行編譯,之後運行編譯文件(*.out),需要進行兩個文件的配置,分別是:launch.json及tasks.json,配置也並不麻煩。
具體步驟如下:
1.隨便新建個文件夾,我的文件夾名爲vscode。
2.運行vscode,可以直接在終端中輸入code運行。
3.在vscode中安裝C/C++包,如下圖:
4.新建文件並寫入代碼,按快捷鍵F5,在命令行提示處選擇包含C++或(gdb) Launch的調試項。
5.根據提示打開launch.json。其中configurations中需要進行兩處更改。以下代碼中注視處爲更改:
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out", //此處修改,運行目錄下編譯後生成的a.out文件
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask": "c_pre_build", //添加此條,配置在運行前進行編譯,引號中的名字隨便取
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}]
}
6.此時在.vscode文件夾下,已經生成了launch.json文件,在該文件夾下新建文件:tasks.json。其中的配置如下:
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName":"c_pre_build",//這裏是launch.json中配置的preLaunchTask字段
"command": "g++",
"args": [
"-g",
"${file}"
]}
]}
FINAL:回到代碼中按下F5,調試成功!