ubuntu18.04 VSCode配置cmake编译C++程序,并gdb调试程序

本文介绍了在Ubuntu18.04系统中如何安装必要的软件,如cmake、gcc、VSCode和gdb,并详细讲解了如何在VSCode中配置C++项目,包括安装C++相关插件、创建CMakeLists.txt文件,以及编辑tasks.json、c_cpp_properties.json和launch.json三个关键配置文件,以实现C++代码的编译和gdb调试功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.ubuntu18.04环境软件安装

  • 安装cmake
  • 安装gcc
  • 安装VSCode
  • 安装gdb调试工具

以上安装不做具体说明

2. VSCode 使用cmake编译C++代码配置,gdb调试生成程序

2.1 VSCode安装插件:

进入此Extensions(Ctrl+Shift+X)菜单,install 以下item:C/C++,  C++ Intellisense, CMake,CMake Tools

2.2 配置工程

cmake编译代码,gdb调试程序主要在于三个文件的生成与配置,分别为tasks.json, c_cpp_properties.json, launch.json。

使用快捷键Ctrl+shift+p 弹出添加配置项。

以自己的项目为例:

我的项目目录:

CMakeLists.txt 内容为:


   
  1. #CMake 要求的至少版本
  2. cmake_minimum_required(VERSION 3.10)
  3. # set the project name and version
  4. project(Tutorial VERSION 1.0)
  5. add_definitions(-std =c + + 17)
  6. #构建的输出类型,executable是app,library是动态库,最后的.cpp 是被构建的文件名
  7. add_executable(Tutorial Tutorial.cpp)
  8. # add_library(Tutorial Tutorial.cpp)

2.2.1  launch.json文件


choose Run > Add Configuration... and then choose C++ (GDB/LLDB).
配置如下:


   
  1. {
  2. / / Use IntelliSense to learn about possible attributes.
  3. / / Hover to view descriptions of existing attributes.
  4. / / For more information, visit: https: / / go.microsoft.com /fwlink /?linkid = 830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "(gdb) Launch",
  9. "type": "cppdbg",
  10. "request": "launch",
  11. / / "program": "${fileDirname}/${fileBasenameNoExtension}",
  12. "program": "${workspaceFolder}/build/Tutorial",
  13. "args": [],
  14. "stopAtEntry": false,
  15. "cwd": "${workspaceFolder}",
  16. "environment": [],
  17. "externalConsole": true,
  18. "MIMode": "gdb",
  19. "setupCommands": [
  20. {
  21. "description": "Enable pretty-printing for gdb",
  22. "text": "-enable-pretty-printing",
  23. "ignoreFailures": true
  24. }
  25. ],
  26. "preLaunchTask": "Tutorial", / /与tasks.json 的label项目同名
  27. "miDebuggerPath": "/usr/bin/gdb"
  28. }
  29. ]
  30. }

2.2.2 tasks.json

添加tasks.json

并修改内容如下:


   
  1. {
  2. "version": "2.0.0",
  3. "tasks": [
  4. {
  5. "label": "Tutorial", / /编译的项目名,build
  6. "type": "shell",
  7. "command": "cd ./build ;cmake .. ;make", / /编译命令
  8. "group": {
  9. "kind": "build",
  10. "isDefault": true
  11. }
  12. },
  13. {
  14. "label": "clean",
  15. "type": "shell",
  16. "command": "make clean",
  17. }
  18. ]
  19. }

2.2.3 c_cpp_properties.json

添加c_cpp_properties.json文件

Ctrl+Shift+P,输入C/C++,选择C/C++: Edit Configurations(JSON)

内容如下:


   
  1. {
  2. "configurations": [
  3. {
  4. "name": "Linux",
  5. "includePath": [
  6. "${workspaceFolder}/**"
  7. ],
  8. "defines": [],
  9. "compilerPath": "/usr/bin/gcc",
  10. "cStandard": "gnu11",
  11. "cppStandard": "gnu++17",
  12. "intelliSenseMode": "gcc-x64",
  13. "configurationProvider": "ms-vscode.cmake-tools"
  14. }
  15. ],
  16. "version": 4
  17. }

全部保存,配置完成。

3. 测试结果

开始编译Ctrl+Shift+B

编译输出:

打断电即可gdb调试:

从上面可以看到,会有堆栈信息,终端输出,变量监测和表达式查看。

4.可能遇到的问题

4.1 可以编译通过,但是在引用头文件时,有波浪号错误显示

解决方案:

方案1:

重新打开该工程目录即:关闭目录再打开

方案2:

在工作区会.vscode文件夹中, 
打开命令行 输入 

gcc -v -E -x c++ -
   

出现:

将此标出菜单内容添加到c_cpp_properties.json文件中,如下


   
  1. {
  2. "configurations": [
  3. {
  4. "name": "Linux",
  5. "includePath": [
  6. "${workspaceFolder}/**",
  7. "/usr/include/c++/7/**",
  8. "/usr/include/x86_64-linux-gnu/c++/7/**",
  9. "/usr/include/c++/7/backward/**",
  10. "/usr/lib/gcc/x86_64-linux-gnu/7/include/**",
  11. "/usr/local/include/**",
  12. "/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed/**",
  13. "/usr/include/x86_64-linux-gnu/**",
  14. "/usr/include/**"
  15. ],
  16. "defines": [],
  17. "compilerPath": "/usr/bin/gcc",
  18. "cStandard": "gnu11",
  19. "cppStandard": "gnu++17",
  20. "intelliSenseMode": "linux-gcc-x64",
  21. "configurationProvider": "ms-vscode.cmake-tools"
  22. }
  23. ],
  24. "version": 4
  25. }

重新打开文件目录就可恢复正常。

### 安装 VSCode 为了在 Ubuntu 18.04 上安装 Visual Studio Code (VSCode),可以按照官方推荐的方法通过命令行完成。打开终端执行以下命令: ```bash sudo apt update sudo apt install software-apt-repository --yes ppas:vim/+vim curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list' sudo apt-get install apt-transport-https sudo apt update sudo apt install code ``` 上述命令将会更新软件源列表,添加 Microsoft 的 GPG 密钥以及 APT 源地址,最终安装最新版本的 VSCode[^1]。 ### 配置 C++ 插件 安装完成后启动 VSCode,在扩展市场中搜索 `C/C++` 插件由微软提供,点击安装即可。该插件提供了 IntelliSense、调试支持等功能来增强 C++ 编程体验。 ### 创建工程项目结构 对于简单的项目可以直接创建文件夹作为工作区;而对于复杂一点的应用程序则建议采用 CMake 或其他构建工具管理依赖关系和编译流程。这里以最基础的方式为例说明如何新建一个 Hello World 工程: #### 步骤如下: 1. 打开 VSCode 新建文件夹命名为 project_name; 2. 在该项目目录内建立 src 文件夹放置源码文件 hello.cpp; 3. 使用内置终端编写简单测试代码: ```cpp #include <iostream> int main() { std::cout << "Hello, world!" << std::endl; return 0; } ``` 4. 接着在同一级目录下创建 launch.json 和 tasks.json 来定义调试配置与任务运行参数。 launch.json 示例: ```json { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/src/hello", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "build" } ] } ``` tasks.json 示例: ```json { "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-g", "${workspaceFolder}/src/hello.cpp", "-o", "${workspaceFolder}/src/hello" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "Generated task to build the program using g++." } ] } ``` 以上即完成了基本的 C++ 开发环境搭建过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值