首頁 >開發工具 >VSCode >vscode如何單步調試

vscode如何單步調試

藏色散人
藏色散人原創
2019-12-24 10:38:096347瀏覽

vscode如何單步調試

vscode如何單步驟偵錯?

vscode單步調試

VScode配置

0.快捷鍵

Ctrl + `  打开默认终端;
Ctrl + Shift + `  新建新的终端;
Ctrl + Shift + Y  打开调试控制台,然后再自行切换终端选项;
ps: ` 在键盘数字1的左边。

相關推薦:vscode入門教學

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)   #添加语句

以上是vscode如何單步調試的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn