Configuring launch.json, task.json and settings.json for debugging in VS Code with git bash as default terminal? - visual-studio-code

I am trying to configure the Debugger in VSCode
I looked through official documentation to setup the VSCode debugger for C/C++ but it's not working.
Documentation states the steps for setting up vscode debugger for
powershell in windows.
But I am trying to set-up debugger with git bash as my default integrated terminal in windows.
I have added git bash as default terminal but I am not able to setup debugger with git bash as integrated terminal.
Default configuration files :
launch.json
{
"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:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
The property :
"externalConsole": false
is set to false as I want VSCode to use the integrated default bash terminal instead of using external terminal for debugging.
tasks.json :
{
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\MinGW\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "gnu++14",
"intelliSenseMode": "clang-x86"
}
],
"version": 4
}
settings.json
{
"files.associations": {
"iostream": "cpp"
},
"C_Cpp.errorSquiggles": "Disabled"
}
With the above configuration, when I start the debugging it gives me the following error:
It seems like the command property in tasks.json is incorrect,
as bash converts
"C:\MinGW\bin\g++.exe" -> "C:MinGWbing++.exe"
and gives error: "no command found" because back-slash('\') in bash is an escape character.
Now changing above path in tasks.json to bash style :
"command": "C:/MinGW/bin/g++.exe"
resolves the above error, but now it gives same error for variable ${file} as this path-variable gets dynamically set to current open file .cpp.
I have been working on this debugging issue for last few days, but haven't found any workaround yet.
How can I change/update by configuration files to use git bash as default integrated terminal in VSCode for debugging.

It seems like the command property in tasks.json is incorrect, as bash converts
"C:\MinGW\bin\g++.exe" -> "C:MinGWbing++.exe"
Then try a cygwin-like path:
/c/MingW/bin/g++.exe
# or
/C/MingW/bin/g++.exe
Check if it is interpreted correctly by the git bash session then.

Related

How do I make vs code put the output of my c program in TERMINAL panel?

I'm trying to build and run C code with vscode on windows 10.
I've gone through the vscode doc for mingw configuration, followed the steps there and managed to run a .c file with vscode.
However, there's still an issue yet.
each time run my program via "Run | Run Without Debugging", the panel switches automatically to "TERMINAL"
So I have to switch to the DEBUG CONSOLE manually each time I run the code, which is tediously boring.
Is there a way to keep the "DEBUG CONSOLE" panel active or show the output of my program in "TERMINAL" panel
I also tried the suggestion in another stackoverflow post, but it doesn't work for me.
tasks.json
Here is my tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "E:\\MinGW\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: E:\\MinGW\\bin\\gcc.exe"
}
]
}
launch.json
Here is my 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": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "E:\\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"
}
]
}
How do I make vs code put the output of my c program in TERMINAL panel?
You can use Code Runner with some simple configuration.
Install Code Runner.
Type Ctrl + Shift + P
Search and open Open Settings(JSON)
Add the following json snippets to your settings.json:
"code-runner.runInTerminal": true
Every time you want to run your c code, just type the icon from the upper right corner that Code Runner provide.
For step 4, you can also open vscode settings and change it on GUI.

vscode cpp build tasks.json type:"cppbuild" error

VSCode : 1.61.0
Linux: Ubuntu 20.04.3 LTS
I'm trying to build c++ program using by VSCode.
when the run build, VSCode showing this message.
I know build tasking need matching launch.json file's preLaunchTask with tasks.json file's label. and I'm already set it.
tasks.json
{
"tasks": [
{
"type": "cppbuild", // but "shell" is working
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
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": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
The problem is, if I change the "type" value in tasks.json, from shell to cppbuild, It doesn't work and show the error message like to.
showing error message when the run build
what is different "cppbuild" and "shell"? and What should I do how using "cppbuild" value?
Te values of cppbuild are fine. Try changing in launch.json the value:
"preLaunchTask": "C/C++: g++ build active file",
to:
"preLaunchTask": "cppbuild",
The difference between shell and cppbuild is that the first open the BASH and runs a command you write, the second, cppbuild, runs an specific binary (or program).

pipeTransport program can't be found when debugging with vscode and wsl

I tried compiling and debugging c++ programs in vscode with wsl, compiling succeeded, but when I tried to press F5 to debug, the error is that the pipe program failed to start. Here is my launch.json.
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "/home/maxu/projects/helloworld/helloworld.out",
"args": [""],
"stopAtEntry": true,
"cwd": "/home/maxu/projects/helloworld/",
"environment": [],
"externalConsole": true,
"windows": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"pipeTransport": {
"pipeCwd": "",
"pipeProgram": "c:\\windows\\sysnative\\bash.exe",
"pipeArgs": ["-c"],
"debuggerPath": "/usr/bin/gdb"
},
"sourceFileMap": {
"/mnt/c": "${env:systemdrive}/",
"/usr": "C:\\Users\\maxu1\\AppData\\Local\\Packages\\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\\LocalState\\rootfs\\usr"
}
}
]
}
here is the error message
error message
I tried to modify the parameter "pipeProgram" to "c:\windows\system32\bash.exe", but also failed.
I have a similar problem as yours except that my pipe program is ssh.exe under c:\windows\system32\openssh.
Here is how I work through it: copy openssh folder to c:\ and add that path to my user path environment variable. You can have a try.
I don't know if the windows system folders are accessible from vscode.

Debugging using Visual Studio Code and piping terminal output to a file

I am using VS Code to debug an application on Ubuntu, using a launch.json file and cmake to build and debug. This works fine and I can see the output of the program in the terminal as expected. However, I would like to automatically save this output to a file. The way I would do this typically would be something like mycommand > terminal_output.txt, however I can't find a way of replicating this using the launch.json file, or alternatively of running the debug through the terminal (e.g. something along the lines of debug --flags launch.json > terminal_output.txt).
Here is my launch.json for reference:
{
"version": "0.2.0",
"configurations": [
{
"name": "g++-8 build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++-8 build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
Is there a way of doing this in a simple way?
Since I am using cmake, I was able to achieve this using cmake.debugConfig in my settings.json file:
{
"cmake.debugConfig": {
"args": [
">",
"test.txt"
]
}
}
Adding "args" in launch.json, however, did not work.
install CodeLLDB extension
launch.log: inside a CodeLLDB launch configuration add
"stdio": [null, null, "debug.log"] // stdin/stdout/stderr
stdio redirection for CodeLLDB

Chain pre-launch tasks in VS Code

Is it possible to invoke more than one pre-launch task using VS Code?
I try to restore packages then build then run but I can only get to configure build.
My launch.json:
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/src/myProject/bin/Debug/netcoreapp1.0/myProject.dll",
"args": [],
"cwd": "${workspaceRoot}/src/myProject",
"stopAtEntry": false,
"externalConsole": false
},
My tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [
"./**/project.json"
],
"isBuildCommand": true,
"showOutput": "always",
"problemMatcher": "$msCompile"
}
]
}
so I tried to specify the dotnet restore command however it does not work.
I know this is long over due. However, I think I figured out the solution. Steps I did.
Create a workspace to include all the projects in the workspace.
Go to Run and Debug, and click on "Add Config (workspace)
the format is the following:
{
"folders": [
{
"path": "project1"
},
{
"path": "project2"
},
],
"launch": {
"version": "0.2.0",
"compounds": [{
"name": "Chain Stars",
"configurations": [
"ConfigurationName1",
"ConfigurationName2",
]
}]
}
}
ConfigurationName1, and ConfigurationName2 is the profile name you would like to put them in sequence to launch your website.
4. Save the profile. In this case. "Chain Stars" is going to show up in the profile name for you to run it. Let me know if you have any questions.