ubuntu 搭建 cmake + vscode 的 c/c++ 开发环境

打印 上一主题 下一主题

主题 860|帖子 860|积分 2580

软件安装


最基本的 vscode 插件

只需要安装如下两个插件即可
c/c++ 扩展是为了最基本的代码提示和调试支持
cmake language support 是为了提示 CMakeLists.txt 脚本


代码

main.cpp
  1. #include <stdio.h>
  2. int main()
  3. {
  4.     printf("\nhello world\n\n");
  5.     return 0;
  6. }
复制代码
CMakeLists.txt
  1. cmake_minimum_required(VERSION 3.24)
  2. project(hello_ubuntu CXX)
  3. set(CMAKE_CXX_STANDARD 14)
  4. set(CMAKE_CXX_STANDARD_REQUIRED True)
  5. add_executable(${PROJECT_NAME} main.cpp)
复制代码
任务配置
  1. {
  2.     // See https://go.microsoft.com/fwlink/?LinkId=733558
  3.     // for the documentation about the tasks.json format
  4.     "version": "2.0.0",
  5.     "tasks": [
  6.         {
  7.             "label": "build-debug",
  8.             "type": "shell",
  9.             "command": "cmake -S . -B cmake-build-debug -DCMAKE_BUILD_TYPE=Debug && cmake --build cmake-build-debug",
  10.             "dependsOn": [
  11.                 "configure"
  12.             ]
  13.         },
  14.         {
  15.             "label": "build-release",
  16.             "type": "shell",
  17.             "command": "cmake -S . -B cmake-build-release -DCMAKE_BUILD_TYPE=Release && cmake --build cmake-build-release",
  18.             "dependsOn": [
  19.                 "configure"
  20.             ]
  21.         },
  22.         {
  23.             "label": "clean",
  24.             "type": "shell",
  25.             "command": "rm -rf build && rm -rf cmake-build-debug && rm -rf cmake-build-release"
  26.         },
  27.         {
  28.             "label": "rebuild",
  29.             "type": "shell",
  30.             "dependsOn": [
  31.                 "clean",
  32.                 "build-debug",
  33.                 "build-release"
  34.             ]
  35.         },
  36.         {
  37.             "label": "run",
  38.             "type": "shell",
  39.             "command": "./cmake-build-release/hello_ubuntu",
  40.             "dependsOn": [
  41.                 "build-release"
  42.             ]
  43.         }
  44.     ]
  45. }
复制代码
此时可以通过终端菜单的运行任务来运行
改进任务的运行方式

安装如下插件

Task Buttons 插件

.vscode文件夹添加.settings.json,并添加如下内容
  1. {
  2.     "VsCodeTaskButtons.showCounter": true,
  3.     "VsCodeTaskButtons.tasks": [
  4.         {
  5.             "label": "$(notebook-delete-cell) clean",
  6.             "task": "clean"
  7.         },
  8.         {
  9.             "label": "$(debug-configure) rebuild",
  10.             "task": "rebuild"
  11.         },
  12.         {
  13.             "label": "$(notebook-execute) run",
  14.             "task": "run"
  15.         }
  16.     ]
  17. }
复制代码
然后状态栏就会出现对应的按钮, 直接点击任务对应的按钮即可运行任务. 图标从 这里 获取

Task Explorer 插件

此插件将提供了一个任务面板, 安装之后 查看->打开试图 搜索Task Explorer 即可打开此面板, 拖到自己喜欢的位置然后直接点击对应任务右侧的按钮即可运行任务. 任务太多的话, 可以将任务加入 Favorites 列表, 把其他的收起来就可以了

快捷键

参考: https://blog.csdn.net/qq_45859188/article/details/124529266
debug

参考 这里, 直接在 .vscode 文件夹下添加 launch.json
  1. {
  2.   "version": "0.2.0",
  3.   "configurations": [
  4.     {
  5.       "name": "test-debug",
  6.       "type": "cppdbg",
  7.       "request": "launch",
  8.       "program": "${workspaceRoot}/cmake-build-debug/hello_ubuntu",
  9.       "args": [],
  10.       "stopAtEntry": false,
  11.       "cwd": "${workspaceFolder}",
  12.       "environment": [],
  13.       "externalConsole": false,
  14.       "MIMode": "gdb",
  15.       "miDebuggerPath": "/usr/bin/gdb",
  16.       "setupCommands": [
  17.         {
  18.           "description": "Enable pretty-printing for gdb",
  19.           "text": "-enable-pretty-printing",
  20.           "ignoreFailures": true
  21.         }
  22.       ],
  23.       "preLaunchTask": "rebuild"
  24.     }
  25.   ]
  26. }
复制代码
打一个断点, 然后直接 F5

注意: 有时候 vscode 的 debug 会出问题, 此时直接执行 clean 任务再进行调试即可

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

写过一篇

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表