VSCode調試C++

在ubuntu下調試C++ 本人以爲VSCode比較好用。c++

步驟以下:shell

1. 編寫.cpp,.h文件json

自行完成本身的程序。ubuntu

2. 編寫CMakeLists.txt。下面是一個比較好用的模板。bash

根目錄爲:ui

PROJECT(mmseg CXX) #項目名稱

#Cmake最低版本要求
CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 

#容許gdb調試
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

#添加C++11支持及其餘選項
set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -g -ftest-coverage -fprofile-arcs -Wno-deprecated")
#set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS -g -ftest-coverage -fprofile-arcs"})


#添加dict子目錄
add_subdirectory(dict) 

#當前目錄下全部源文件賦給DIR_SRCS
AUX_SOURCE_DIRECTORY(. DIR_SRCS) 

#生成可執行文件
add_executable(mmseg ${DIR_SRCS}) 

#添加C++11編譯選項,可是隻有CMAKE 3.1支持
#target_compile_features(mmseg PRIVATE cxx_range_for)

#添加連接庫
target_link_libraries(mmseg Dict)

子目錄爲:spa

#查找當前目錄下全部源文件
#並將結果保存在DIR_LIB_SRCS變量中
aux_source_directory(. DIR_LIB_SRCS)

#生成連接庫
add_library(Dict ${DIR_LIB_SRCS})

3. 編寫一個compile.shdebug

mkdir -p build
rm -rf build/*
cd build
cmake .. -DCMAKE_VERBOSE_MAKEFILE=ON -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Debug
make

4. build and run tasks調試

使用Ctrl+Shift+P快捷鍵,而且在下拉菜單中選擇>tasks: configure task,而後在下拉菜單中選擇ohter 用於build程序。在生成的tasks.json中加入build與debug內容   code

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "CMake build",
            "type": "shell",
            "command": "./compile.sh",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        },
        {
            "label": "run",
            "type": "shell",
            "command": "./build/your_run_file",  #!! change here for your config
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

  

而後使用Ctrl+Shift+P快捷鍵,而且在下拉菜單中選擇>Tasks: Run Task,選擇build或者run tasks就能夠build並生成可執行文件。

這時可能會提示:

/bin/bash: ./compile.sh: Permission denied
The terminal process terminated with exit code: 126

錯誤,使用命令

sudo chmod 755 compile.sh

5.debug程序

因爲使用cmake來build程序,已經在compile.sh中加入了debug信息-DCMAKE_BUILD_TYPE=Debug

點擊VSCode左側的小蟲子按鈕,添加一個新的configuration,選擇C++(GDB/LLDB)。這時產生一個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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/blob_demo",  #!! change here for your config
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

在你的.cpp文件中設置一個斷點,點擊運行按鈕,就能夠debug了。

相關文章
相關標籤/搜索