軟體安裝
略
最基本的 vscode 插件
只需要安裝如下兩個插件即可
c/c++ 擴展是為了最基本的代碼提示和除錯支持
cmake language support 是為了提示 CMakeLists.txt 腳本
代碼
main.cpp
#include <stdio.h>
int main()
{
printf("\nhello world\n\n");
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
project(hello_ubuntu CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
add_executable(${PROJECT_NAME} main.cpp)
任務配置
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build-debug",
"type": "shell",
"command": "cmake -S . -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug && cmake --build cmake-build-debug",
"dependsOn": [
"configure"
]
},
{
"label": "build-release",
"type": "shell",
"command": "cmake -S . -B cmake-build-release -DCMAKE_BUILD_TYPE=Release && cmake --build cmake-build-release",
"dependsOn": [
"configure"
]
},
{
"label": "clean",
"type": "shell",
"command": "rm -rf build && rm -rf cmake-build-debug && rm -rf cmake-build-release"
},
{
"label": "rebuild",
"type": "shell",
"dependsOn": [
"clean",
"build-debug",
"build-release"
]
},
{
"label": "run",
"type": "shell",
"command": "./cmake-build-release/hello_ubuntu",
"dependsOn": [
"build-release"
]
}
]
}
此時可以通過終端
選單的運行任務
來運行
改進任務的運行方式
安裝如下插件
Task Buttons 插件
.vscode
檔案夾添加.settings.json
,并添加如下內容
{
"VsCodeTaskButtons.showCounter": true,
"VsCodeTaskButtons.tasks": [
{
"label": "$(notebook-delete-cell) clean",
"task": "clean"
},
{
"label": "$(debug-configure) rebuild",
"task": "rebuild"
},
{
"label": "$(notebook-execute) run",
"task": "run"
}
]
}
然后狀態欄就會出現對應的按鈕, 直接點擊任務對應的按鈕即可運行任務. 圖示從 這里 獲取
Task Explorer 插件
此插件將提供了一個任務面板, 安裝之后 查看
->打開試圖
搜索Task Explorer
即可打開此面板, 拖到自己喜歡的位置然后直接點擊對應任務右側的按鈕即可運行任務. 任務太多的話, 可以將任務加入 Favorites
串列, 把其他的收起來就可以了
快捷鍵
參考: https://blog.csdn.net/qq_45859188/article/details/124529266
debug
參考 這里, 直接在 .vscode
檔案夾下添加 launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "test-debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/cmake-build-debug/hello_ubuntu",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "rebuild"
}
]
}
打一個斷點, 然后直接 F5
注意: 有時候 vscode 的 debug 會出問題, 此時直接執行 clean 任務再進行除錯即可
本文來自博客園,作者:laolang2016,轉載請注明原文鏈接:https://www.cnblogs.com/khlbat/p/17454015.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/554424.html
標籤:C++
下一篇:返回列表