vscode远程Linux调试c++程序

前提条件

1、Windows下的vscode可以远程连接linux
2、Linux环境已安装g++、gdb
3、安装gdbserver: sudo apt install gdbserver

假设的程序目录

|----------files
	    |------file1.h
	    |------file1.cc
	    |------file2.h
	    |------file2.cc
|----------tests
		|------main.cc
|----------build
|----------bin (放置可执行文件)
|----------CMakeList.txt

1、配置文件

CMakeList.txt

cmake_minimum_required(VERSION 3.0)
project(files)
option(TEST "ON for complile test" ON) 
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -rdynamic -O0 -ggdb -std=c++17 -Wall -Wno-deprecated -Werror -Wno-unused-function")

set(LIB_SRC 
    file/file1.cc
    file/file2.cc
    )                                     # (变量 文件名/路径/...)

add_library(file SHARED ${LIB_SRC})  # (库文件名称 STATIC 文件)

add_executable(tests tests/main.cc)       # (可执行文件名称 文件)
add_dependencies(tests file)
target_link_libraries(tests file)  # (库文件名称/可执行文件名称 链接的库文件名称)

SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}
launch.json
{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/bin/tests",
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "linux": {
                "MIMode": "gdb",
                "miDebuggerPath": "/usr/bin/gdb",
                "miDebuggerServerAddress": "192.168.1.119:2334",  //linux机器的IP地址,端口号和gdbserver的端口号一致
            },
            "logging": {
                "moduleLoad": false,
                "engineLogging": false,
                "trace": false
            },
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}
task.json
{
	"version": "2.0.0",
	"tasks": [
		{
            "type": "cppbuild",
            "label": "${workspaceFolder}/bin/tests",
            "command": "/usr/bin/gbd",
            "args": [
                "-g",
                "${workspaceRoot}/tests/main.cpp",
                "-o",
                "${workspaceFolder}/bin/tests"  //可执行文件路径
            ],
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
	]
}

2、编译

mkdir build
cd build/
cmake ..
cd ..
cd bin/
./test

vscode终端执行命令:

sudo gdbserver localhost:2334 /home/test/sylar/build/tests  args1 args2 ..
//端口号和launch.json中的端口号一致,若有参数则加入args处

3、设置断点调试

vscode远程Linux调试c++程序_第1张图片

你可能感兴趣的:(linux,vscode,c++)