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

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. }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值