在ubuntu系统上使用vscode 调试c++

使用vs code 调试c++

在linux系统上调试简单项目

如果我们仅仅是写少量的cpp文件,不包含第三方包,可以直接使用g++ 编译.
以下是官网的方法.

vs code 官网上的说明

前提,安装插件c/c++

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控制面板上依次点击configurebuild.

在这里插入图片描述

4. 调试

现在可以在调试面板上进行调试操作了.

小结

  1. 安装插件cmakecmake tools
  2. 舍弃tasks.json文件, 修改launch.json文件
  3. 使用cmake构建项目
  4. 调试
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值