关于ubuntu环境下vscode进行debug的随笔

CMakeLists.txt的编写

顶层目录的CMakelists.txt

  1. 目录:./CMakeLists.txt
bash 复制代码
#./CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(xxx_project_name LANGUAGES CXX)		#设置工程名

# 设置 C++ 标准和编译选项
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -msse4.1")  # 启用 SSE4.1 优化

#配置是debug OR release
if(NOT CMAKE_BUILD_TYPE)
   set(CMAKE_BUILD_TYPE Release)
   message(STATUS "Build type not specified: defaulting to Release.")
endif(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "")

#添加动态库
link_libraries(m)
link_libraries(pthread)

#添加目录所有.c/cpp 到 DIR_SRCS
#aux_source_directory(<dir> <var>)只收集c/cpp、不递归、不感知新增文件
aux_source_directory(. DIR_SRCS) 
#file(GLOB <var> <pattern>) 自定义收集文件、支持递归、新增文件需要重新运行cmake、可能匹配多余文件,如备份文件
file(GLOB SOURCES "*.cpp")       # 手动指定通配符模式
file(GLOB_RECURSE SOURCES "src/**/*.cpp")  # 递归
#set(<var> <filelist>) 维护成本高
set(SOURCES "main.cpp" "util.cpp")  # 显式列出所有文件


include_directories(${PROJECT_BINARY_DIR}/lib/include)
include_directories(${PROJECT_SOURCE_DIR}/lib/include)

# generate executalbe file
add_executable(xxx_project_name ${DIR_SRCS})

# 额外
# 添加 需要子目录
add_subdirectory(lib)
add_subdirectory(utils)
add_subdirectory(common)
# 添加链接库
target_link_libraries(xxx_project_name utils)
target_link_libraries(xxx_project_name ch_estimation)
target_link_libraries(xxx_project_name common)

2.1 ./utils/CMakeLists.txt

bash 复制代码
# ./utils/CMakeLists.txt
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)

# 生成链接库
add_library (utils ${DIR_LIB_SRCS})

./lib/CMakeLists.txt

bash 复制代码
#./lib/CMakeLists.txt

add_subdirectory(src)
add_subdirectory(include)
add_subdirectory(examples)
add_subdirectory(test)

INSTALL(  DIRECTORY include/
          DESTINATION "${INCLUDE_DIR}"
          FILES_MATCHING PATTERN "*.h" )
          

./lib/src/CMakeLists.txt

bash 复制代码
#./lib/src/CMakeLists.txt
add_subdirectory(phy)

./lib/src/phy/CMakeLists.txt

bash 复制代码
#./lib/src/phy/CMakeLists.txt
add_subdirectory(phch)

set(srsran_srcs $<TARGET_OBJECTS:srsran_phch>
        )
#$<TARGET_OBJECTS:target_name> 生成器表达式        
#​功能​:获取指定目标(通常是对象库)编译生成的 ​对象文件列表​(.obj/.o 文件)
​#使用前提​:
#target_name 必须是已定义的对象库(OBJECT 库)
#对象库通过 add_library(... OBJECT) 声明

add_library(srsran_phy STATIC ${srsran_srcs})
target_link_libraries(srsran_phy pthread m ${FFT_LIBRARIES})
install(TARGETS srsran_phy DESTINATION ${LIBRARY_DIR} OPTIONAL)

.\lib\src\phy\phch\CMakeLists.txt

bash 复制代码
# .\lib\src\phy\phch\CMakeLists.txt
file(GLOB SOURCES "*.c")
add_library(xxx_sf1_1_name OBJECT ${SOURCES})
add_subdirectory(sf1_1_1_name)

.\lib\src\phy\phch\test\CMakeLists.txt

bash 复制代码
# .\lib\src\phy\phch\test\CMakeLists.txt
set(CTEST_LABELS "lib;phy;phch")
add_executable(pmch_test pmch_test.c)
target_link_libraries(pmch_test srsran_phy)

vscode::tasks.json

此时需要注意:

  • 如果在CMakeLists.txt文件中对CMAKE_BUILD_TYPE的设置是Release时,想要在vscode进行单步调试,需要在cmake configure这个过程中添加-DCMAKE_BUILD_TYPE=Debug
  • 参数里面的"-S","${workspaceFolder}"等价于
json 复制代码
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "CMake: Configure",
            "type": "cppbuild",
            "command": "cmake",
            "args": [
                "-S", "${workspaceFolder}",
                "-B", "${workspaceFolder}/build",
                // "-G", "Ninja",
                //如果在CMakeLists.txt文件中设置的是
                //set(CMAKE_BUILD_TYPE Release CACHE STRING "")时
                //想step debug,需要如下配置
                "-DCMAKE_BUILD_TYPE=Debug",
                //"-DCMAKE_BUILD_TYPE=Release",
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        },

        {
            "label": "CMake: build",
            "type": "cppbuild",
            "command": "make",
            "args": [
                "-C", "${workspaceFolder}/build",
                "-j", "4",
                "all"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn":["CMake: Configure"],
            "detail": "Task generated by Debugger."
        },
    ],
}

vscode::launch.json

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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/modem",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "CMake: build",
        },
        {
            "name": "(gdb) Launch pmch_test",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/buildM/lib/src/phy/phch/test/pmch_test",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/buildM/lib/src/phy/phch/test/",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "CMake: build",
        },
    ]
}
相关推荐
新时代牛马2 小时前
OpenSSL引擎 + PKCS11 + SoftHSM2认证
linux
皓月盈江3 小时前
国产Linux银河麒麟操作系统安装开源免费Draw.io(diagrams.net)替代Visio
linux·ubuntu·开源·draw.io·visio·银河麒麟操作系统·diagrams.net
2301_793102495 小时前
linux——C程序的编译与调试
linux
三体世界5 小时前
HTTPS加密原理
linux·开发语言·网络·c++·网络协议·http·https
浅浅练习两年半5 小时前
5.3 LED字符设备驱动
linux
우 유6 小时前
【ing】Ubuntu安装Anaconda及环境配置\docker\pycharm
linux·运维·ubuntu
扛麻袋的少年7 小时前
vscode 开发nuxt,点击标签跳转到 components.d.ts 解决方案
vscode·nuxt3
lqjun08277 小时前
在Ubuntu 24.04上安装cuDNN v8.x.x兼容CUDA 11.8
linux·运维·ubuntu
ZKf30FkG8 小时前
在 CentOS 7.9 中 Node 18.20.2 安装指南
linux·运维·centos