I'm trying to create a cpp environment on vs code. The build works, .exe gets generated, but the debugger doesn't seem to work. When I click on the run debug option, another terminal gets open with a message (attached in the screenshot). The code which I'm running is a simple hello world example. No terminal with Hello World is opened by the debugger. Hello world is supposed to be displayed in the screenshot of terminal 2 but another message gets printed, which I got no clue about.
Terminal 1
Terminal 2
This is the code for the config files:
task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"sample.cpp"
],
"problemMatcher": []
}
]
}
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": "C:\\Users\\Amey\\Desktop\\work\\Programming\\C++\\a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
Related
I am having a problem setting up my debugger in VSCode for C++. I wrote some code, then ran into some errors while running it so I decided to debug. I think thats when VSCode created the task.json file. Then I realized I wanted to use command line arguments and google/stackoverflow said to create a launch.json file which I did. It was empty so I pressed the add configuration button and got this.
{
// 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": "enter program name, for example ${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/path/to/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
}
]
}
]
}
Now I tried to fill the placeholders but I can't do them without getting tons of errors after errors. Could someone please post an example of a working launch.json and how the file paths are supposed to work?
My main goal is to debug my code while using command line arguments.
Here is a sample from a project I currently use:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch Test",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/build_amd64/bin/tester_config",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}/build_amd64",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": []
}
]
}
If you post your launch.json with the filled in values then I might be able to tell you exactly what's wrong, because assuming you filled out everything correctly, you should be working.
You can find tutorials how to set up Visual Studio Code for C++ here:
https://code.visualstudio.com/docs/cpp/config-msvc
https://code.visualstudio.com/docs/cpp/config-mingw
A valid config should look something like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"problemMatcher": ["$msCompile"],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}
As you can see, it's best to let the available extensions handle the setup, as getting all the compiler switches right can be tricky.
The full reference for tasks.json is found here:
https://code.visualstudio.com/docs/editor/variables-reference
New to this stuff and vscode wont let me debug my C++ file. Whenever i hit F5, i get the following error:
Unable to start debugging. Unexpected GDB output from command "-environment-cd"d:\Users\MyName\\OneDrive\\Documentos\\My Projects"". d:\Users\MyName\OneDrive\Documentos\My Projects: No such file or directory
This is my launch.json file. Ive already tried this solution with no results.
{
// 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 - Compilar y depurar el archivo activo",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Strawberry\\c\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Habilitar la impresión con sangrÃa para gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe compilar archivo activo"
}
]
}
When ever I debug my code in Visual Studio Code I get
The preLaunch 'C/C++:gcc.exe build active file' terminated with exit code -1
I built my file using cpp.exe,
here is the launch.json file :
{
// 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": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "C://code//program.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]
}
When I run the debug, the output from my program isn't displayed on the integrated terminal.
This actually used to work a few days ago. I haven't changed any config files and yet, perhaps due to an update, this has changed.
Note that there is no "console" option with lldb.
This also creates a major issue that I can't provide input to my running program.
This is what I see in the integrated terminal:
[1] + Done "/usr/bin/lldb-mi" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-871ntlpp.93i" 1>"/tmp/Microsoft-MIEngine-Out-msz8v24g.agq"
And that's it. The case is the same when using "externalConsole":true.
How do I keep whatever output is currently in the debug terminal inside it, but also see the normal program output and be able to give input in the integrated terminal?
Also, this behavior leads me to think, currently, what is even the point of having the integrated terminal? Nothing is happening there.
here are my config files:
"version": "0.2.0",
"configurations": [
{
"name": "clang++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "clang++ build active file",
"miDebuggerPath": "/usr/bin/lldb-mi"
}
]
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
unfortunately
https://code.visualstudio.com/docs/cpp/launch-json-reference
macOS: When set to true, it will spawn an external console through lldb-mi. When set to false, the output can be seen in VS Code's debugConsole. Due to limitations within lldb-mi, integratedTerminal support is not available.
I found a question here that is pretty close, but the C++ extension has been updated since then and the launch.json file that is created for you is very different and I'm not sure how to set it up. There also isn't a lot of info online to do this, so I'm hoping someone has done this before. My current project structure is like this:
MainProj
./build
CMakeLists.txt
main.cpp
otherClasses.hpp
And the cmake target that shows up in build after running ..cmake and make is called Main.
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++ build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
This is the tasks.json file from the other question
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "cd build && cmake .. && make"
}
]
}