c++ vscode cmake debug for mac

  1. 下载vscode

  2. 安装c++插件

参考:C++ programming with Visual Studio Code

  1. 安装llvm,可以使用brew安装

  2. 配置llvm到系统环境变量中

  3. 编写c++代码

  4. 编写CMakeLists.txt文件(前提安装cmake)

    cmake_minimum_required(VERSION 3.5)

    project(vscode_cmake_debug)

    设置可执行文件的输出路径

    set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

    重点

    set(CMAKE_BUILD_TYPE "Debug")

    add_executable(main src/main.cpp)

  5. 生成vscode中c++的智能提示文件c_cpp_properties.json

可参考:https://code.visualstudio.com/docs/cpp/config-clang-mac

复制代码
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/opt/homebrew/opt/llvm/bin/clang",
            "intelliSenseMode": "macos-clang-arm64"
        }
    ],
    "version": 4
}
  1. 生成vscode debug文件launch.json

具体可参考:Configure VS Code for Clang/LLVM on macOS

复制代码
{
    // 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": "Debug",
            "type": "lldb",
            "request": "launch",            
            "program": "${workspaceFolder}/cmake_debug/bin/main",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            //"preLaunchTask": "C/C++: g++ build active file",
            //"miDebuggerPath": "lldb的路径"
        }
    ]
}

其中部分字段配置可参考:

https://code.visualstudio.com/docs/editor/variables-reference

Configure launch.json for C/C++ debugging in Visual Studio Code

因为已经采用cmake编译,所以launch中的preLaunchTask可以不用关联task了

参考文件:

c++程序的 cmake编译+vscode来debug 方法和步骤(ubuntu)_cmake 如何debug-CSDN博客

相关推荐
爱凤的小光18 小时前
图漾GM461-E1相机专栏
c++
qwepoilkjasd19 小时前
C++智能指针介绍
c++
·白小白19 小时前
力扣(LeetCode) ——43.字符串相乘(C++)
c++·leetcode
咬_咬19 小时前
C++仿muduo库高并发服务器项目:Poller模块
服务器·开发语言·c++·epoll·muduo
FMRbpm19 小时前
链表5--------删除
数据结构·c++·算法·链表·新手入门
Kimser20 小时前
QT C++ QWebEngine与Web JS之间通信
javascript·c++·qt
QT 小鲜肉20 小时前
【QT/C++】Qt样式设置之CSS知识(系统性概括)
linux·开发语言·css·c++·笔记·qt
Elias不吃糖20 小时前
NebulaChat 框架学习笔记:深入理解 Reactor 与多线程同步机制
linux·c++·笔记·多线程
转基因21 小时前
命名空间与匿名空间
c++
煤球王子21 小时前
学而时习之:C++中的动态内存管理
c++