Mac VSCode CMAKE C++ 代码调试

[TOC]

安装VSCode插件(C/C++、codelldb)

配置CMakeLists.txt(debug配置)

cmake_minimum_required(VERSION 3.2)

PROJECT(zsign)
# debug调试配置
SET(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")

# 引入头文件
INCLUDE_DIRECTORIES(
    /Users/yangzhao/openssl/include
)
# 动态链接库
LINK_DIRECTORIES(
    /Users/yangzhao/openssl/lib
)

FILE(GLOB SRC_LIST_CPP ${PROJECT_SOURCE_DIR}/common/*.cpp)

AUX_SOURCE_DIRECTORY(. SRC_LIST_CPP)

ADD_EXECUTABLE(${PROJECT_NAME} ${SRC_LIST_CPP})

TARGET_LINK_LIBRARIES(${PROJECT_NAME} crypto)

launch.json配置

{
    // 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": "build and debug active file",
            "type": "lldb",
            "request": "launch",
            "targetArchitecture":"x64",
            "program": "${workspaceFolder}/build/zsign",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "preLaunchTask": "make"
        },
    ]
}

type一定要设置为lldb Mac平台使用lldb调试工具

program cmake编译安装后的可执行程序路径

externalConsole 是否打开控制台(关闭即可)

preLaunchTask 执行文件前需要的编译任务(见tasks.json配置)

tasks.json配置

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceRoot}/build"
    },
    "tasks": [
        {
            "type": "shell",
            "label": "cmake clean",
            "command": "rm -rf *",
        },
        {
            "type": "shell",
            "label": "cmake",
            "command": "cmake",
            "args": [".."],
            "dependsOn":["cmake clean"]
        },
        {
            "label": "make",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "type": "shell",
            "command": "make -j8",
            "args": [],
            "dependsOn": [ "cmake" ],
        }
    ]
}

dependsOn 依赖任务的label名称(根据自己项目实际情况配置任务即可)

以上属于原创文章,转载请注明作者@怪咖

你可能感兴趣的:(Mac VSCode CMAKE C++ 代码调试)