VS Code C ++调试 [英] VS Code C++ debugging

查看:26
本文介绍了VS Code C ++调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试调试C ++程序,但它向我显示此错误:

I try to debug my C++ programs but it shows me this error:

找不到任务"g ++构建活动文件".

Could not find the task 'g++ build active file'.

我尝试通过选择C/C ++来添加Visual Studio代码在其网站上所说的配置:(gdb)附加,但是没有任何变化.

I tried adding a configuration as Visual Studio Code says in its website, by choosing C/C++: (gdb) Attach, but nothing changes.

推荐答案

我费了好几天的时间才能在VS Code中为C ++设置运行和调试环境.最后,我找到了解决问题的方法.请按照以下步骤先决条件-在您的PC上安装Mingw32.

I struggled for days to setup a running and debugging environment for C++ in VS Code. Finally I found a way to solve the problem. Please follow the below steps Prerequisites- Have Mingw32 installed on your PC.

  1. 为VS代码安装C ++扩展

  1. Install C++ extension for VS code

为VS代码安装竞争性编程帮助程序扩展.就像是这将显示在左侧面板上.

Install Competitive programming helper extension for VS code. Something like this will appear on the left panel.

打开您的.cpp文件,并通过单击上图中突出显示的区域来打开测试运行器.输入输入,然后单击上图中的runall按钮,您将能够看到输出.

Open your .cpp file and open the test runner by clicking on the highlighted are in the above image. Enter the inputs and click on the runall button in the above image you will be able to see the output.

用于调试:-

1.单击下图中的调试按钮.您将获得一个说明运行并调试文件"的选项.点击那个.

1.Click on the debug button in the below image . You would get an option stating "Run and Debug file". Click on that.

  1. 它将创建一个launch.json文件和task.json文件.我们正在使用竞争性编程帮助程序扩展名来运行cpp文件时,请删除task.json文件.

  1. It would create a launch.json file and task.json file. Delete the task.json file as we are using competitive programming helper extension for running our cpp files.

下面是launch.json的内容,用于设置调试器环境将上述相同的配置粘贴到launch.json文件中.

Below is the content of launch.json to setup the debugger environment Paste the same exact configuration mentioned above in your launch.json file.

对于"miDebuggerPath",请提供PC上mingw32文件夹内gcc.exe文件的位置.

For the "miDebuggerPath", please provide the location of the gcc.exe file inside mingw32 folder on your pc.

  1. 在args键中,我使用了"args":[",," $ {fileDirname} \ input.txt",",," $ {fileDirname} \ output.txt],上面的代码用于在调试时简化输入和输出的过程中从input.txt文件获取输入并将输出写入output.txt文件.请在所有C ++文件所在的文件夹中创建一个input.txt和output.txt.

  1. In the args key I have used "args": ["<", "${fileDirname}\input.txt",">", "${fileDirname}\output.txt"], The above code is use to take input from input.txt file and write output to output.txt file while debugging for the easy input and output. Please create a input.txt and output.txt in the same folder where you have all your C++ files.

在.cpp文件中添加一个断点,然后单击绿色的RUN按钮运行调试器,调试器将启动,它将从input.txt中获取所有输入,并将输出写入output.txt中调试会话结束后.

Add a breakpoint in your .cpp file and run the debugger by click on the green RUN button , your debugger would start and it would take all the input from input.txt and write the output in the output.txt after the debugging session ends.

快乐编码.

这篇关于VS Code C ++调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆