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 内容为:

#CMake 要求的至少版本
cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(Tutorial VERSION 1.0)

add_definitions(-std=c++17)

#构建的输出类型,executable是app,library是动态库,最后的.cpp 是被构建的文件名
add_executable(Tutorial Tutorial.cpp)
#add_library(Tutorial Tutorial.cpp)

2.2.1  launch.json文件


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

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            // "program": "${fileDirname}/${fileBasenameNoExtension}",
            "program": "${workspaceFolder}/build/Tutorial",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Tutorial",  //与tasks.json 的label项目同名
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

2.2.2 tasks.json

添加tasks.json

并修改内容如下:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Tutorial",//编译的项目名,build
            "type": "shell",
            "command": "cd ./build ;cmake .. ;make",//编译命令
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "make clean",
            

        }
    ]
}

2.2.3 c_cpp_properties.json

添加c_cpp_properties.json文件

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

内容如下:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "gcc-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

全部保存,配置完成。

3. 测试结果

开始编译Ctrl+Shift+B

编译输出:

打断电即可gdb调试:

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

4.可能遇到的问题

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

解决方案:

方案1:

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

方案2:

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

gcc -v -E -x c++ -

出现:

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

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/c++/7/**",
                "/usr/include/x86_64-linux-gnu/c++/7/**",
                "/usr/include/c++/7/backward/**",
                "/usr/lib/gcc/x86_64-linux-gnu/7/include/**",
                "/usr/local/include/**",
                "/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed/**",
                "/usr/include/x86_64-linux-gnu/**",
                "/usr/include/**"

            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值