C++ 环境安装:

sudo apt update
sudo apt install build-essential gdb
sudo apt install cmake


gcc --version
g++ --version
gdb --version
cmake --version

C/C++ Themes
CMake   写CMakeLists.txt能提示
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch",
            "type": "cppdbg",
            "request": "launch",
            # 要调试的文件路径
            "program": "/home/lgc/demo/07_calib/OpenCalib-master/lidar2camera/manual_calib/bin/run_lidar2camera", // 编译后的可执行文件
            // 要调试的参数
            "args": ["data/0.png","data/0.pcd","data/center_camera-intrinsic.json","data/top_center_lidar-to-center_camera-extrinsic.json"], //
            "stopAtEntry": false,
            //先切换到什么路径,与参数有关
            "cwd": "${workspaceFolder}/lidar2camera/manual_calib",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            //"preLaunchTask": "build"  //如果不用编译,可以不要
        }
    ]
}
task.josn
{
	"version": "2.0.0",
	"options": {
		"cwd": "${workspaceFolder}/build"    //需要进入到我们执行tasks任务的文件夹中
	},
	"tasks": [    //tasks包含三个小任务
		{
			"type": "shell",
			"label": "cmake",    //第一个任务的名字叫cmake
			"command": "cmake",    //它要执行的命令是cmake
			"args": [
				"-DCMAKE_BUILD_TYPE=Debug",
				".."    //参数是..
			]
		},
		{
            "type": "shell",
			"label": "make",    //第二个任务的名字叫make
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"command": "make",    //它要执行的命令是make
			"args": [],
            "options": {
                "cwd": "${workspaceFolder}/build",
            },
		},
		{
			"label": "build",    //第三个任务的名字叫build
			"dependsOrder": "sequence",    //顺序执行依赖项
			"dependsOn":[    //依赖的两个项为cmake和make
				"cmake",    //即第一个任务的label
				"make"      //即第二个任务的label
			]
		}
	]
}
setting.json:要调试的cmake文件
{
    "cmake.sourceDirectory": "/home/lgc/demo/07_calib/OpenCalib-master/lidar2camera/manual_calib"
// 整体界面缩放(相当于 Ctrl+/- 的效果)
"window.zoomLevel": 1.5,
// 可选:单独调整编辑器字体
"editor.fontSize": 20,
// 可选:调整终端字体
"terminal.integrated.fontSize": 15
}

终端-->运行生成任务-->生成tasks.json ,主要是减少cmake指令

{
	"version": "2.0.0",
	"tasks": [
		// 1. cmake 配置
		{
			"type": "shell",
			"label": "CMake: 配置",
			"command": "cmake",
			"args": [
				"-S",
                "${workspaceFolder}",
				"-B",
                "${workspaceFolder}/build",
                "-DCMAKE_BUILD_TYPE=Debug"
			],
			"problemMatcher": "$msCompile",
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"options": {
				"cwd": "${workspaceFolder}"
			}
		},
		// 2. cmake 构建
		{
			"type": "shell",
			"label": "CMake: 构建",
			"command": "cmake",
			"args": [
				"--build",
				"${workspaceFolder}/build"
			],
			"problemMatcher": "$msCompile",
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"dependsOn": [
				"CMake: 配置"
			]
		},
		// 3. 删除Build目录
		{
			"type": "shell",
			"label": "删除Build目录",
			"command": "rm",
			"args": [
				"-rf",
				"build"
			],
			"problemMatcher": "$msCompile",
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"options": {
				"cwd": "${workspaceFolder}"
			}
		},
		// 4. 运行可执行文件
		{
			"type": "shell",
			"label": "运行可执行文件",
			"command": "./Build/main",
			"problemMatcher": "$msCompile",
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"dependsOn": [
				"CMake: 构建"
			]
		}
		
	]
}
launch.json  #可以用which gdb 查看dgb的路径
gdb --version  查看gdb的版本
点CMake Configure即可调试CMakeLists.txt文件,还要安装CMake Tools工具

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ CMake Debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/Build/${fileBasenameNoExtension}", // 编译后的程序,需要结合CMakeLists.txt中的add_executable()函数
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "/usr/bin/gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "CMake: 构建"
        },{
            "name": "CMake Debug",
            "type": "cmake",
            "request": "launch",
            "scriptPath": "/home/standard/yolo/ncnn/CMakeLists.txt",
            "cmakeDebugType": "script",
        },
        {
            "name": "CMake Configure",
            "type": "cmake",
            "request": "launch",
            "cmakeDebugType": "configure"
        }

    ]
}
c_cpp_properties.json

vscode调试时查找头文件的路径

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/opencv4"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

如何断点调试CMakeLists.txt文件

参考文档:https://github.com/microsoft/vscode-cmake-tools/blob/51b7ced409a4e0e26c036d4284d5077cbad66318/docs/debug.md

1、首先安装cmake tools,cmake的版本版本必须大于3.27

2、launch.json中填写

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "cmake",
            "request": "launch",
            "name": "Debug CMake Configure (yolov8)",
            "cmakeDebugType": "configure", # 如果是调试cmake文件,就是选这个
            "clean": true,
            "configureAll": false
        }
    ]
}

3、设置settings.json

{
  "cmake.sourceDirectory": "${workspaceFolder}/examples/yolov8/cpp",
  "cmake.buildDirectory": "${workspaceFolder}/build/cmake-debug/yolov8-rk3588",
  "cmake.generator": "Unix Makefiles",
    "cmake.configureSettings": {  # cmake的参数
    "CMAKE_BUILD_TYPE": "Release",
    "THIRD_PARTY_LIB_DIR": "/workspace/src/perception-platform/build/linux-arm64-jetson/third_party/lib",
    "CMAKE_PROJECT_INCLUDE": "/workspace/src/perception-platform/cmake/PerceptionSubprojectEnvironment.cmake",
    "PERC_PREBUILD_PLATFORM": "linux-arm64-jetson",
    "PERC_PREBUILD_DIR": "/workspace/src/perception-platform/third_party/prebuild/linux-arm64-jetson",
    "PERC_RUNTIME_PROFILE": "jetson",
    "PERC_PREBUILD_PROFILE": "jetson",
    "perc_infra_DIR": "/workspace/src/perception-platform/build/linux-arm64-jetson/infra/build",
    "perc_pipeline_DIR": "/workspace/src/perception-platform/build/linux-arm64-jetson/pipeline/build",
    "perc_scheduler_DIR": "/workspace/src/perception-platform/build/linux-arm64-jetson/scheduler/build"
  }
}


这些项的原理:
cmake.sourceDirectory 指定真正包含 CMakeLists.txt 的源码目录。因为 rknn_model_zoo 根目录不是直接构建 yolov8,而是它下面的 examples/yolov8/cpp。
cmake.buildDirectory 指定构建输出目录。这样不会把中间文件散落到源码目录。
cmake.generator 指定生成器,这里是 Unix Makefiles,所以后面通常走 make。
cmake.configureArgs 这就是你手敲命令时 -Dxxx=yyy 的图形化版本。CMake Tools 点“配置”时,会自动把这些参数带上。
一句话理解:这份 settings.json 是在告诉 VSCode/Cursor,“如果你帮我跑 cmake,请按这套参数跑”。

4、点CMake Configure即可断点调试CmakeLists.txt文件

断点调试roslaunch文件,不调可执行程序

launch.json的写法:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug easygo_perception.launch.py",
      "type": "debugpy",  // 使用Python调试器
      "request": "launch",  // 表示由一个新进程来调试
      "program": "/opt/ros/humble/bin/ros2",  // 使用这个ros2
      "args": [
        "launch",
        "/home/standard/code/Easygo/code/easygo/src/easygo_perception_bridge/launch/easygo_perception.launch.py",
        "enable_dcw2_upper_camera:=false",
        "enable_dcw2_lower_camera:=false",
        "enable_perception:=false"
      ],
      "cwd": "/home/standard/code/Easygo/code/easygo",
      "console": "integratedTerminal", // 让命令跑在VSCode集成终端里
      "justMyCode": false,   //允许调试进入第三方库代码
      "env": {
        "EASYGO_ROOT": "/home/standard/code/Easygo/code/easygo",
        "PYTHONUNBUFFERED": "1"
      }
    }
  ]
}

launch.py

import os

from launch import LaunchDescription
from launch.actions import (
    DeclareLaunchArgument,
    ExecuteProcess,
    SetEnvironmentVariable,
    TimerAction,
)
from launch.conditions import IfCondition
from launch.substitutions import EnvironmentVariable, LaunchConfiguration, PathJoinSubstitution
from launch_ros.actions import Node


def generate_launch_description():
    easygo_root = os.environ.get("EASYGO_ROOT", "/home/std/project/easygo")
    default_runtime_root = os.path.join(
        easygo_root, "third_party", "perception_platform", "current"
    )

    runtime_root = LaunchConfiguration("perception_platform_root")

    def dcw2_camera_launch(prefix):
        camera_name = LaunchConfiguration(f"dcw2_{prefix}_camera_name")
        enabled = IfCondition(LaunchConfiguration(f"enable_dcw2_{prefix}_camera"))
        container = Node(
            package="rclcpp_components",
            executable="component_container",
            name="camera_container",
            namespace=camera_name,
            output="screen",
            condition=enabled,
        )
        load_component = TimerAction(
            period=1.0,
            actions=[
                ExecuteProcess(
                    cmd=[
                        "bash",
                        "-lc",
                        [
                            "ros2 component load /",
                            camera_name,
                            "/camera_container orbbec_camera orbbec_camera::OBCameraNodeDriver ",
                            "--node-name ob_camera_node --node-namespace /",
                            camera_name,
                            " -p camera_name:=",
                            camera_name,
                            " -p serial_number:=",
                            LaunchConfiguration(f"dcw2_{prefix}_serial_number"),
                            " -p device_num:=",
                            LaunchConfiguration("dcw2_device_num"),
                            " -p enable_color:=",
                            LaunchConfiguration("dcw2_enable_color"),
                            " -p enable_depth:=",
                            LaunchConfiguration("dcw2_enable_depth"),
                            " -p enable_ir:=",
                            LaunchConfiguration("dcw2_enable_ir"),
                            " -p enable_point_cloud:=",
                            LaunchConfiguration("dcw2_enable_point_cloud"),
                            " -p depth_registration:=",
                            LaunchConfiguration("dcw2_depth_registration"),
                            " -p align_mode:=",
                            LaunchConfiguration("dcw2_align_mode"),
                            " -p color_width:=",
                            LaunchConfiguration("dcw2_color_width"),
                            " -p color_height:=",
                            LaunchConfiguration("dcw2_color_height"),
                            " -p color_fps:=",
                            LaunchConfiguration("dcw2_color_fps"),
                            " -p depth_width:=",
                            LaunchConfiguration("dcw2_depth_width"),
                            " -p depth_height:=",
                            LaunchConfiguration("dcw2_depth_height"),
                            " -p depth_fps:=",
                            LaunchConfiguration("dcw2_depth_fps"),
                            " -p color_format:=",
                            LaunchConfiguration("dcw2_color_format"),
                            " -p depth_format:=",
                            LaunchConfiguration("dcw2_depth_format"),
                        ],
                    ],
                    output="screen",
                    condition=enabled,
                )
            ],
        )
        return [container, load_component]

    upper_camera_launch = dcw2_camera_launch("upper")
    lower_camera_launch = dcw2_camera_launch("lower")
    ros2_setup = PathJoinSubstitution([runtime_root, "ros2", "setup.bash"])
    launch_cmd = [
        "bash",
        "-lc",
        [
            "if [ -d /home/std/easygo_ws/install/easygo_follow_msgs ]; then "
            "export AMENT_PREFIX_PATH=/home/std/easygo_ws/install/easygo_follow_msgs:${AMENT_PREFIX_PATH:-}; "
            "export CMAKE_PREFIX_PATH=/home/std/easygo_ws/install/easygo_follow_msgs:${CMAKE_PREFIX_PATH:-}; "
            "export LD_LIBRARY_PATH=/home/std/easygo_ws/install/easygo_follow_msgs/lib:${LD_LIBRARY_PATH:-}; "
            "fi; "
            "source ",
            ros2_setup,
            " && exec ros2 launch easygo_perception_ros2 easygo_perception.launch.py ",
            "perception_platform_root:=",
            runtime_root,
            " pipeline_config_path:=",
            LaunchConfiguration("perception_config_path"),
            " max_frames:=",
            LaunchConfiguration("perception_max_frames"),
        ],
    ]

    return LaunchDescription(
        [
            DeclareLaunchArgument("enable_perception", default_value="true"),
            DeclareLaunchArgument("enable_dcw2_upper_camera", default_value="true"),
            DeclareLaunchArgument("enable_dcw2_lower_camera", default_value="true"),
            DeclareLaunchArgument("perception_mode", default_value="current"),
            DeclareLaunchArgument(
                "perception_platform_root",
                default_value=EnvironmentVariable(
                    "PERCEPTION_PLATFORM_ROOT", default_value=default_runtime_root
                ),
            ),
            DeclareLaunchArgument(
                "perception_config_path",
                default_value=PathJoinSubstitution(
                    [runtime_root, "config", "pipelines", "pedestrian_detection_orbbec_yolov8.yaml"]
                ),
            ),
            DeclareLaunchArgument("perception_max_frames", default_value="0"),
            DeclareLaunchArgument("dcw2_upper_camera_name", default_value="camera_upper"),
            DeclareLaunchArgument("dcw2_upper_serial_number", default_value="CH8J1620106"),
            DeclareLaunchArgument("dcw2_lower_camera_name", default_value="camera_lower"),
            DeclareLaunchArgument("dcw2_lower_serial_number", default_value="CH8J16202R4"),
            DeclareLaunchArgument("dcw2_device_num", default_value="2"),
            DeclareLaunchArgument("dcw2_enable_color", default_value="true"),
            DeclareLaunchArgument("dcw2_enable_depth", default_value="true"),
            DeclareLaunchArgument("dcw2_enable_ir", default_value="false"),
            DeclareLaunchArgument("dcw2_enable_point_cloud", default_value="false"),
            DeclareLaunchArgument("dcw2_depth_registration", default_value="true"),
            DeclareLaunchArgument("dcw2_align_mode", default_value="HW"),
            DeclareLaunchArgument("dcw2_color_width", default_value="640"),
            DeclareLaunchArgument("dcw2_color_height", default_value="480"),
            DeclareLaunchArgument("dcw2_color_fps", default_value="10"),
            DeclareLaunchArgument("dcw2_depth_width", default_value="640"),
            DeclareLaunchArgument("dcw2_depth_height", default_value="400"),
            DeclareLaunchArgument("dcw2_depth_fps", default_value="10"),
            DeclareLaunchArgument("dcw2_color_format", default_value="MJPG"),
            DeclareLaunchArgument("dcw2_depth_format", default_value="Y11"),
            SetEnvironmentVariable("PERCEPTION_PLATFORM_ROOT", runtime_root),
            SetEnvironmentVariable(
                "LD_LIBRARY_PATH",
                [
                    PathJoinSubstitution([runtime_root, "lib"]),
                    ":",
                    PathJoinSubstitution([runtime_root, "lib", "plugins"]),
                    ":",
                    EnvironmentVariable("LD_LIBRARY_PATH", default_value=""),
                ],
            ),
            *upper_camera_launch,
            *lower_camera_launch,
            TimerAction(
                period=3.0,
                actions=[
                    ExecuteProcess(
                        cmd=launch_cmd,
                        output="screen",
                        condition=IfCondition(LaunchConfiguration("enable_perception")),
                    )
                ],
            ),
        ]
    )

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐