Reputation: 314
I try to debug my C++ programs but it shows me this error:
Could not find the task 'g++ build active file'.
I tried adding a configuration as Visual Studio Code says in its website, by choosing C/C++: (gdb) Attach, but nothing changes.
Upvotes: 0
Views: 1817
Reputation: 36
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.
Install C++ extension for VS code
Install Competitive programming helper extension for VS code. Something like
this will appear on the left panel.
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.
For debugging :-
1.Click on the debug button in the below image . You would get an option stating “Run and Debug file”. Click on that.
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.
Below is the content of launch.json to setup the debugger environment Paste the same exact configuration mentioned above in your launch.json file.
For the “miDebuggerPath”, please provide the location of the gcc.exe file inside mingw32 folder on your pc.
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.
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.
Happy Coding.
Upvotes: 2
Reputation: 150
Unfortunately, cannot add a comment, so I have to submit an "answer", which is merely a guess. Did you download "mingw64" with g++? Probably, you did not add mingw64/bin to your system path. Control panel -> Edit path -> Environment variables -> Path -> New.. (it is also in the tutorial link below) To really make sure the things run, check this tutorial, by Microsoft: https://code.visualstudio.com/docs/cpp/config-mingw
Perhaps, uninstall your downloaded g++ and install this mingw64 repository. Quite helpful. I use it myself and for a quick debugging - works like clocks!
Upvotes: 1