vs++linux+win,VSCode在Linux和Windows调试C/C++程序

VSCODE是一个文本编辑器,不具备编译功能,但是可以调用外部编译器调试器来实现调试功能

Linux 使用GDB,Windows 可以安装minGW,

写好测试文件main.cpp , 用vscode 打开代码所在文件夹,切换到调试界面,点击配置按钮

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

2fc4afefe19e31c64c56eb6026190714.png

选择 C++(GDB/LLDB) for Windows or Linux

或者选择 C++(Windows) for Windows

弹出默认的launch.json文件,在此基础上修改

0818b9ca8b590ca3270a3433284dd417.png

3a3baf8b18b38a6477b344ca9577d6be.png

“program” : 要调试运行的程序,修改为程序名,或 {file}.o / {file}.exe

根据当前系统在"linux"或者“Windows” 分别添加 gdb的安装路径

"miDebuggerPath": "/usr/bin/gdb",

"miDebuggerPath": "D:/MinGW/bin/gdb.exe",

下面多余的 C++ Attach项可以整个删除,也可忽略,修改好后的launch.json文件如下

//launch.json

{

"version": "0.2.0",

"configurations": [

{

"name": "C++ Launch",

"type": "cppdbg",

"request": "launch",

"program": "${file}.o",

"args": [],

"cwd": "${workspaceRoot}",

"environment": [],

"externalConsole": true,

"preLaunchTask": "g++",

"linux": {

"miDebuggerPath": "/usr/bin/gdb",

"MIMode": "gdb",

"setupCommands": [

{

"description": "Enable pretty-printing for gdb",

"text": "-enable-pretty-printing",

"ignoreFailures": true

}

]

},

"osx": {

"MIMode": "lldb"

},

"windows": {

"miDebuggerPath": "D:/MinGW/bin/gdb.exe",

"MIMode": "gdb",

"setupCommands": [

{

"description": "Enable pretty-printing for gdb",

"text": "-enable-pretty-printing",

"ignoreFailures": true

}

]

}

}

]

}

然后点击调试按钮,提示 没有配置task runner,点击配置

0818b9ca8b590ca3270a3433284dd417.png

c27cd487653cb9cd6d8fd1a7185b836f.png

0818b9ca8b590ca3270a3433284dd417.png

e26419d14fb57a36d960f74892d5d481.png

选择Others或任一个,弹出默认tasks.json文件,用下面的文件覆盖原来的内容

//tasks.json

{

"version": "0.1.0",

"command": "g++",

"args": [

"-g",

"${file}",

"-o",

"${file}.o"

],

"problemMatcher": {

"owner": "cpp",

"fileLocation": [

"relative",

"${workspaceRoot}"

],

"pattern": {

"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",

"file": 1,

"line": 2,

"column": 3,

"severity": 4,

"message": 5

}

}

}

command: gcc , g++

args: 编译参数

最后再次点调试按钮,即可成功运行调试,可以添加断点,查看变量,堆栈等常用调试功能

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值