0.快捷键
-
稍大工程在vscode下的调试参考该博客。
Ctrl +
打开默认终端; Ctrl + Shift +
新建新的终端;
Ctrl + Shift + Y 打开调试控制台,然后再自行切换终端选项;
ps: ` 在键盘数字1的左边。
1.安装clang
sudo apt-get install clang
2.VScodeDebug
插件参考链接但大型的工程配置还是比较复杂,很多教程都是针对单个cpp文件进行配置,这里对vscode配置一般情况的cpp调试做个记录:
1.整体思路就是首先按照常规方法进行mkdir build && cd build && cmake.. && make
(这一步可以在vscode的终端完成,也可以在系统终端完成,无所谓。但是为了少开点界面就在vscode里面完成比较好)生成可执行文件,然后使用vscode进行单步。
2. 配置launch文件:点击左侧边栏的Debug图标(Ctrl+Shift+D),再点上方的齿轮图标configure
,点击default configure
就能自动生成launch.json文件。进入launch文件界面可以看到路径为.vscode/launch.json/Launch Targets/(gbd)Launch
.
3.修改launch文件:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/app/testMonoBA", //此路径更改为最终生成的可执行文件路径以及可执行文件名称
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole":false,//将此处的true改为false,不然他会调用系统的终端进行现实
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
}
]
}
4.修改CMakeLists.txt文件:
cmake_minimum_required(VERSION 2.8)
project(slam_demo)
set(DEFAULT_BUILD_TYPE "Debug") #修改处,讲release改为debug,也可以直接删除
if (NOT CMAKE_BUILD_TYPE)
message(STATUS "Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified.")
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}" CACHE
STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
"Debug" "Release" "MinSizeRel" "RelWithDebInfo")
endif ()
set(CMAKE_CXX_FLAGS "-std=c++11")
FIND_PACKAGE( OpenMP REQUIRED)
if(OPENMP_FOUND)
message("OPENMP FOUND")
ADD_DEFINITIONS(-DUSE_OPENMP)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -Wno-reorder" CACHE STRING "" FORCE)
#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -DNDEBUG -Wno-reorder -O2" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS_DEBUG "{CMAKE_CXX_FLAGS} -o0 -ggbd") #添加语句
add_compile_options(-g) #添加语句
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
option(BUILD_APPS "Build APPs for slam course" YES)
option(BUILD_TESTS "Build test for slam course" No)
# third party libs
# eigen
find_package(Eigen REQUIRED)
include_directories(${EIGEN_INCLUDE_DIR})
# opencv
find_package(OpenCV 4 REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
# glog
find_package(Glog REQUIRED)
include_directories(${GLOG_INCLUDE_DIRS})
# sophus
include_directories(${PROJECT_SOURCE_DIR}/thirdparty/Sophus)
include_directories(${PROJECT_SOURCE_DIR})
add_subdirectory(frontend)
add_subdirectory(backend)
add_subdirectory(utils)
if (BUILD_APPS)
add_subdirectory(app)
endif ()
if (BUILD_TESTS)
enable_testing()
add_subdirectory(test)
endif ()
5.完成以上后,重新进行编译,然后vscode中的调试一栏下面的就不再式灰色可以进行单步调试。
如图:
3.Cmake支持gdb调试的方法
SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
add_compile_options(-g) #添加语句