文章目录
使用vs code 调试c++
在linux系统上调试简单项目
如果我们仅仅是写少量的cpp文件,不包含第三方包,可以直接使用g++ 编译.
以下是官网的方法.
前提,安装插件c/c++
1. 设置编译器路径
按ctrl+shift+p
打开命令面板
.
如上图所示,选择c/c++:edit configurations(UI)
设置
Compiler path
/usr/bin/g++IntelliSense mode
gcc-x64
设置完成后自动生成.vscode\c_cpp_properties.json
文件
文件内容如下
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
在 vscode中将鼠标放在name上,可以显示其注释
2. 创建构建任务(build task)
按ctrl+shift+p
打开命令面板
. 输入Tasks: Configure Default Build Task.
选择使用模版创建task.json文件
> 选择others
自动生成的task.json
文件内容 如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello"
}
]
}
将其修改为:
{
"version": "2.0.0",
"tasks": [
{
"label": "helloworld",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-std=c++11",
"-o",
"./helloworld.out",
"helloworld.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
参数中的
-g
表示启用调试模式编译, 否则无法调试
3. 配置调试选项
在调试面板中选择添加配置
,然后选择c/c++(GDB/LLDB)
最后会生成launch.json
文件,内容如下:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "enter program name, for example ${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
需要稍作修改:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/helloworld.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
4. 添加源代码
在项目目录中新建helloworld.cpp
文件,写入如下内容:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello", "C++", "World", "from", "VS Code!", "and the C++ extension!"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
设置点断,点击调试:
一直点击单步跳过执行完毕后会弹出提示
无法打开“libc-start.c”: 无法读取文件(Error: 找不到文件(/build/glibc-LK5gWL/glibc-2.23/csu/libc-start.c))。
不知到为何会有该问题,可能是bug
小结
为了 能够调试,我们需要在.vscode
下建立三个文件:
c_cpp_properties.json
: 该文件是c/c++ 插件
进行代码提示和纠错的设置,与编译无关.tasks.json
:该文件是设置编译的配置文件,command
参数,设置编译器,args
参数,设置编译器参数. 按按Ctrl + Shift + B
键进行编译.launch.json
: 调试器的配置文件,program
设置要调试的程序,MIMode
设置调试器.
在linux系统上调试复杂项目
如果要调试的项目比较复杂,使用g++编译将会费事, 所以可以使用cmake
进行项目构建.
1. 安装插件
Cmake
插件: 可以对cmakellists的编写,进行代码提示和纠错.CMake Tools
插件: 提供一些cmake快捷命令
插件安装完成后.打开命令面板,输入: Cmake: quick start
,输入项目名称,即可新建CMakeLists.txt
文件. 其内容如下:
cmake_minimum_required(VERSION 3.0.0)
project(helloword VERSION 0.1.0)
# 手动添加的,否则,在ubuntu16.04中不会使用c++11
add_definitions(-std=c++11)
include(CTest)
enable_testing()
add_executable(helloword main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
将add_executable(helloword main.cpp)
改为: add_executable(helloword helloworld.cpp)
手动添加一条命令,使编译器使用c++11标准
add_definitions(-std=c++11)
2. 修改launch.json文件
在这里, tasks.json
文件作废,因为我们不在使用tasks.json
的配置文件进行编译.
修改launch.json
文件, 修改program
参数指定的可执行文件路径
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/helloworld",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
3. 在cmake控制面板上依次点击configure
和build
.
4. 调试
现在可以在调试面板上进行调试操作了.
小结
- 安装插件
cmake
和cmake tools
- 舍弃
tasks.json
文件, 修改launch.json
文件 - 使用cmake构建项目
- 调试