I'm trying to debug a C++ file in VSCode, I followed all the instructions of VSCode but this always shows up. I am using Windows 10. If I click Debug Anyway It will not hit any breakpoint. How do I fix it? The error.
This is my cpp file (so you can also see the explorer tab).
This is the code
#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;
return 0;
}
This is the directory to the c++ file.
C:\Users\ADMIN\Desktop\Program\CppNew\helloworld.cpp
I use this option to debug in VSCode
g++.exe
{ //tasks.json
"tasks": [
{
"type": "cppbuild",
"label": "BuildProject",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\helloworld.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
{ //launch.json
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "BuildProject"
}
]
}
My Debugging Console Log
=thread-group-added,id="i1"
GNU gdb (GDB) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param="pagination",value="off"
Thread 1 hit Breakpoint 1, 0x00007ff7e987146c in main ()
Loaded 'C:\Windows\SYSTEM32\ntdll.dll'. Symbols loaded.
Loaded 'C:\Windows\System32\kernel32.dll'. Symbols loaded.
Loaded 'C:\Windows\System32\KernelBase.dll'. Symbols loaded.
Loaded 'C:\Windows\System32\msvcrt.dll'. Symbols loaded.
Loaded 'C:\msys64\mingw64\bin\libgcc_s_seh-1.dll'. Symbols loaded.
Loaded 'C:\msys64\mingw64\bin\libstdc++-6.dll'. Symbols loaded.
Execute debugger commands using "-exec <command>", for example "-exec info registers" will list registers in use (when GDB is the debugger)
Single stepping until exit from function main,
which has no line number information.
[Thread 11160.0x2ff4 exited with code 0]
[Thread 11160.0x459c exited with code 0]
[Thread 11160.0x37bc exited with code 0]
[Inferior 1 (process 11160) exited normally]
The program 'c:\Users\ADMIN\Desktop\Program\CppNew\helloworld.exe' has exited with code 0 (0x00000000).
These are my g++, gcc, and gdb versions.
g++ (Rev7, Built by MSYS2 project) 12.2.0
gcc (Rev7, Built by MSYS2 project) 12.2.0
GNU gdb (GDB) 12.1
This is my directory to bin (also in my PATH)
C:\msys64\mingw64\bin
I tried reinstalling mingw through msys2 but it still didn't work. I tried putting absolute values in
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
but it says my file does not exist.
Update:
I change this from tasks.json
"options": {
"cwd": "C:\\Users\\ADMIN\\Desktop\\Program\\CppNew"
},
Also changed this from launch.json
"program": "C:\\Users\\ADMIN\\Desktop\\Program\\CppNew\\helloworld.exe",
"cwd": "C:\\Users\\ADMIN\\Desktop\\Program\\CppNew",
It is also active when I try to click the debug button.
Tried restarting VSCode after I input the absolute values.
Still not working. Same error.
Related
VSCode is displaying the error message
ERROR: During startup program exited with code 0xc0000135.
When I try to debug the code. Initially MinGW installation was showing the error missing dll files, then I reinstalled MinGW, now it is no more showing any errors and the catalogue is updating properly. Restarted the system, re-added the bin folder to the Environment Path Variable.
The file is being compiled and executing properly, the Error message is displayed only while debugging
IDE: VSCode
Compiler: MinGW
OS: Windows
Tried Fixes:
Reinstalled MinGW
Added the bin folder to the environment path variables
Restarted VSCode
launch.json for VSCode:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:\\C++\\minGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
Code:
#include <iostream>
int main(){
std::cout << "Hello World" << std::endl;
}
I'm setting up a C environment on a Mac to implement some numerical codes for my phd.
I'm using the Intel C/C++ Classic compiler instead of the default clang.
So far, I manage to generate some debugging information evoking a command like icc -std=c17 -o code -g code.c
When I call the Run and Debug option in VSCode it show 2 options to me: C++(GDB/LLDB) and C++ (Windows). When I click the first one it shows 2 more options: C/C++: clang build and debug active file or C/C++: gcc build and debug active file. It does not show anything related to the Intel Classic Compiler. How do I use this compiler to debug with Intel C/C++ Classic compiler inside the VSCode environment?
Thanks in advance!
I think you are mixing compiling and debugging up, according to the documentation, choosing C/C++: gcc build and debug active file from the list of detected compilers on your system is just helping you to generate some configuration like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": ["-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}"],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
If you want to debug in VSCode, what you need to do is simply adding this configuration to your launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/code",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
Overview
I have been trying to get C++ debugging to work in VS Code for quite some time now with no success.
I followed the official guide from Microsoft on setting up debugging in VS Code, but still ran into problems.
For context:
VS Code config files
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The issue
The problem branches out into two parts.
1. Firstly, I normally use git bash as my default shell in Windows, and would like to keep it that way when debugging C++. However, something that seems to be specific to it is an issue with paths. It seems that running the task here leads to the shell ignoring slashes between directories (likely because of the disparate file system interpretations). This can be seen in the following example:
> Executing task: 'C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe' -g C:\Users\Redact\Desktop\code\Competitive_Programming\Evaluation.cpp -o C:\Users\Redact\Desktop\code\Competitive_Programming\Evaluation.exe <
g++.exe: error: C:UsersRedactDesktopcodeCompetitive_ProgrammingEvaluation.cpp: No such file or directory
g++.exe: fatal error: no input files
compilation terminated.
The terminal process "C:\Program Files\Git\bin\bash.exe '-c', ''C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe' -g C:\Users\Redact\Desktop\code\Competitive_Programming\Evaluation.cpp -o C:\Users\Redact\Desktop\code\Competitive_Programming\Evaluation.exe'" terminated with exit code: 1.
2. When running the debugger with either PowerShell or cmd as the shell, pre-build works as intended and a debugging session is opened, but nothing else happens. The code doesn't stop in the main function (as the guide stated it should) nor are any breakpoints hit—indicating it's not running at all:
(Click Here to see moving gif if it appears still here)
Any help is very much appreciated.
I'm using Visual Studio Code on Windows 10 with the Linux Subsystem. (Ubuntu)
I've created a small c++ file and when I'm building it with the bash terminal - only a .out file is created. Which is fine, but I want to debug it as well and in that case - I can only open .exe files.
When I switch to powershell from bash - the build extension is an .exe
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "g++",
"args": [
"-g", "main.cpp"
],
"group": { "kind": "build", "isDefault": true }
}
]
}
launch.json for debugging
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.exe", // .out doesn't work here
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"preLaunchTask": "echo",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
main.cpp
#include <iostream>
int main() {
std::cout << "hello a" << std::endl;
}
I'm really not sure what to do - as I'm not able to debug .out files and I'd like to choose the build extension myself.
Found the solution:
sudo apt-get install mingw-w64
And then inside tasks.json
"command": "i686-w64-mingw32-g++"
That compiles a 32 bit exe - but the 64 bit version with x86_64-w64-mingw32-g++ somehow doesn't work. Creates an invalid exe.
In order to run/debug cross-platform, your have to use cross-compilers such as MinGW. You can install MinGW-w64 in both Windows, Linux (or WSL).
VScode-tools with gdb.exe from MinGW can debug *.out as well as *.exe files (Tested).
To compile 64-bit version you need to include some static depedency library for C/C++ which are libgcc and libstdc++ while compile. so the command and args in task.json should be:
"command": "x86_64-w64-mingw32-g++",
"args": [
"-g", //Need for debug and compatibility
"-static-libgcc", //flag for the libgcc
"-static-libstdc++", //flag for the libgc++
"helloworld.cpp", //C++ source code
"-o", //flag for output
"a2.out" //Output filename and extension (can be .exe)
],
You can learn more about C/C++ Standard Library Dependencies here
I was following instructions from here. Installed cpptools. Created tasks.json with following contents:
{
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
"showOutput": "always",
"args": ["-g", "helloworld.c"]
}
And launch.json with following content:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (Windows)",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": false,
"windows": {
"MIMode" : "gdb",
"miDebuggerPath": "C:\\Mahesh\\Program Files\\mingw\\MinGW\\bin\\gdb.exe"
}
},
{
"name": "C++ Attach (Windows)",
"program": "${workspaceRoot}/a.exe",
"type": "cppvsdbg",
"request": "attach",
"processId": "${command.pickProcess}",
"windows": {
"MIMode" : "gdb",
"miDebuggerPath": "C:\\Mahesh\\Program Files\\mingw\\MinGW\\bin\\gdb.exe"
}
}
]
}
When I do Ctrl+Shift+B, the code builds, generating a.exe. When I run debug, it gives following output:
--------------------------------------------------------------------------------
You may only use the C/C++ Extension for Visual Studio Code with Visual Studio
Code, Visual Studio or Xamarin Studio software to help you develop and test your
applications.
--------------------------------------------------------------------------------
Loaded 'C:\Mahesh\repos\VSCodeC\polyaddition\a.exe'. Symbols are not loaded.
Loaded 'C:\Windows\System32\ntdll.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\kernel32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\KernelBase.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\sysfer.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\msvcr100.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\QIPCAP64.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\oleaut32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\ole32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\msvcrt.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\gdi32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\user32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\lpk.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\usp10.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\rpcrt4.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\imm32.dll'. Symbols are not loaded.
Loaded 'C:\Windows\System32\msctf.dll'. Symbols are not loaded.
The thread 9524 has exited with code 0 (0x0).
Hello World!!!
The program '[7876] a.exe' has exited with code 0 (0x0).
But the code is not hitting the debug point I set up in the code. You can see, it is printing "Hello World!!!". How can I configure so that it will allow me to step through the code while debugging?
Environment:
The program '[7876] a.exe' has exited with code 0 (0x0),
configured as "i686-pc-mingw32".
gcc.exe (x86_64-win32-seh-rev201506, mingwpy build) 4.9.2
g++.exe (x86_64-win32-seh-rev201506, mingwpy build) 4.9.2
Update
In the discussion in the comments, I have been asked to run gcc with m32 flag as my compiler is 64-bit, it may be generating 64-bit binaries. But gcc -m32 helloworld.c gave errors like this. The comment here explains it with -m32 option. It asks to add i686-w64-mingw32/x86_64-w64-mingw32 flags while compiling. But gcc -x86_64-w64-mingw32 helloworld.c gives language not recognized error, gcc -i686-w64-mingw32 helloworld.c gives unrecognized command line option. What I am doing wrong?
Also this article says that debugging is currently possible only with linux but not with Windows. Is it so?
I believe you are trying to use the VS Code debugger (cppvsdbg) instead of gdb (cppdbg.) This modified launch.json works for me with TDM-GCC and gdb as the debugger:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"linux": {
"program": "${workspaceRoot}/a.out",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"miDebuggerPath": "C:\\TDM-GCC-64\\bin\\gdb.exe",
"program": "${workspaceRoot}\\a.exe",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
},
{
"name": "C++ Attach",
"miDebuggerPath": "C:\\TDM-GCC-64\\bin\\gdb.exe",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceRoot}/a.exe",
"processId": "${command:pickProcess}",
"linux": {
"MIMode": "gdb",
"program": "${workspaceRoot}/a.out",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"osx": {
"MIMode": "lldb"
},
"windows": {
"MIMode": "gdb",
"miDebuggerPath": "C:\\TDM-GCC-64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
}
]
}
If you want command.PickProcess to work..
It should be a ':' not a '.' - therefore:
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
Should sort you out :)
I have faced this issue before. In my case, the compiler generated a release application as default. It has no symbols for debugging.
So, please make sure that you are generated a debug app for debugging.
good luck!