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

相关推荐
_GR7 分钟前
Qt开发④Qt常用控件_上_QWdget属性+按钮类控件
开发语言·c++·qt
Paddi9301 小时前
Codeforces Round 1004 (Div. 1) C. Bitwise Slides
c++·算法
流星白龙6 小时前
【C++】36.C++IO流
开发语言·c++
靡不有初1117 小时前
CCF-CSP第31次认证第二题——坐标变换(其二)【NA!前缀和思想的细节,输出为0的常见原因】
c++·学习·ccfcsp
YH_DevJourney9 小时前
Linux-C/C++《C/7、字符串处理》(字符串输入/输出、C 库中提供的字符串处理函数、正则表达式等)
linux·c语言·c++
和光同尘@9 小时前
1011. A+B和C (15)-PAT乙级真题
c语言·开发语言·数据结构·c++·算法
xianwu54310 小时前
反向代理模块kd
开发语言·网络·数据库·c++·mysql
鄃鳕11 小时前
C++11 thread
开发语言·c++
wen__xvn11 小时前
每日一题洛谷P5738 【深基7.例4】歌唱比赛c++
java·c++·算法
十秒耿直拆包选手12 小时前
cmake:定位Qt的ui文件
c++·qt·ui·cmake