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博客

相关推荐
Forget the Dream40 分钟前
设计模式之责任链模式
java·c++·设计模式·责任链模式
停止重构41 分钟前
【开源】低代码 C++程序框架,Linux多线程程序
c++·低代码·多线程·开源框架·云计算引擎
Duramentee44 分钟前
C++ 设计模式 十二:责任链模式 (读书 现代c++设计模式)
c++·设计模式·责任链模式
_GR1 小时前
Qt开发⑧Qt的窗口_下_浮动窗口+对话框
开发语言·css·c++·qt·microsoft
萌の鱼1 小时前
leetcode 3183. 达到总和的方法数量
数据结构·c++·算法·leetcode
Forget the Dream2 小时前
设计模式之代理模式
java·c++·设计模式·代理模式
攻城狮7号4 小时前
【第十节】C++设计模式(结构型模式)-Flyweight( 享元)模式
c++·设计模式·享元模式
Forget the Dream4 小时前
设计模式之享元模式
java·c++·设计模式·享元模式
溟洵8 小时前
【C/C++】理解C++内存与Linux虚拟地址空间的关系---带你通透C++中所有数据
linux·c语言·c++
Chandler2410 小时前
C++11 智能指针:unique_ptr、shared_ptr和weak_ptr 功能特性 模拟实现
开发语言·c++