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

本文详细指导了在Ubuntu 18.04上使用CMake配置VSCode,并配合GDB进行C++项目开发,包括安装必要的软件、设置VSCode插件、配置tasks.json、c_cpp_properties.json和launch.json,以及解决头文件问题。

摘要生成于 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 内容为:

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

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

### 安装 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++ 开发环境搭建过程。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值