Setting up VS Code for C using Cygwin64 Compiler and Debugger on Windows (ERROR: Unable to start debugging) - c

I am trying to set up VSCODE to debug a C program on Windows using Cygwin64.
I used the config suggested by #stephw ( Setting up VS Code for C using Cygwin64 Compiler and Debugger on Windows ), but it did not work for me.
I cannot comment on the original post, because I do not have enough reputation points, and I cannot answer because I do not know the answer to the original question.
The name of the script is dirigir.c and I can compile. The file dirigir.exe is created. But...
I get the following error:
ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". Error creating process /usr/bin/E:\cloud\Backup\Trabalhos com programas\C and Cpp\scripts/e:\cloud\Backup\Trabalhos com programas\C and Cpp\scripts\dirigir.exe, (error 2).
For some reason, /usr/bin/... / is inserted in the path and the path to the .exe is duplicated.
My launch.json file is as recommended:
{
// 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": "${workspaceFolder}",
"environment": [
{
"name": "PATH",
"value": "%PATH%;z:\\cygwin64\\bin"
}
],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"logging": { "engineLogging": true }, //optional
"preLaunchTask": "gcc.exe build active file"
}
]
}
And my tasks.json is as follows:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "C:\\cygwin64\\bin\\gcc.exe",
"args": [
"-g",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-Werror", // Optional
"-Wall", // Optional
"-Wextra", // Optional
"-ansi", // Optional
"-pedantic", // Optional
"${file}"
],
"options": {
"cwd": "C:\\cygwin64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
]
}
Do you have any idea how to proceed? Thank you in advance.

Finally, I got it working.
Based on WardenGnaw's answers in this thread:
[cppdbg] Cygwin 10.1-1: intergatedTerminal unable to take input #6475 and, of course, #stephw's answer in the original question this one is based, I can debug my C program.
First, I saved a VSCODE workspace in the same folder where my c programs are.
Then, I used the latest Cygwin gdb version (10.2-1). Did not work (I received the error message that made ask this question). Then, I tried 9.2-1 and now it is working.
It is important to remember to add "C:\cygwin64\bin" to PATH.
I changed my launch.json just a little bit. Notice the "preLaunchTask" key does not have the exact same value that the "label" key in the tasks.json has:
{
// 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": "${workspaceFolder}",
"environment": [
{
"name": "PATH",
"value": "%PATH%;C:\\cygwin64\\bin"
}
],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\cygwin64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"logging": { "engineLogging": true }, //optional
"preLaunchTask": "gcc.exe build active file"
}
]
}
My tasks.json is this (notice my name in the end, in the "detail" key):
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\cygwin64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Leonardo."
}
]
}
I notice something a little bit strange:
When I press F5, it does not work, because the task created is not found.
When I edited my launch.json file (preLaunchTask key) to the exact same value in the tasks.json file, the "not found" error disappears, but the debugging process does not start.
"preLaunchTask": "C/C++: gcc.exe build active file"
If I press the Run and debug play button, the debug process does not work either.
When I use the Command Palette (pressing CTRL+SHIFT+P), and click C/C++: Build and Debug Active File,
and then choose the task I created (the second one appears to be automatic),
the program is compiled and the debug process is started.
This is my very simple test script:
#include <stdio.h>
#include <stdlib.h>
int main() {
/* declare and initialize string */
char myString[] = "This is an example of string declaration!";
/* print string */
printf("%s", myString);
int numberA = 5;
int numberB = 10;
int sum = numberA + numberB;
/* Printing a number */
printf("A soma de numberA + numberB é: %d", sum);
return 0;
}
I hope this question and answer can help someone in the future. Peace.

None of the .json changes described here worked for me with gdb 10.2-1. The only thing that did work was the downgrade to gdb 9.2-1, which solved the problem instantly.

Related

vscode debugger not working with multiple files

When i hit F5 to run debugger in vscode, the debug screen (top bar and terminal) shows for 1 second before it disappears. This seems to only happen when I call functions that are defined in another c file. For example, this is not working:
main.c
#include <stdio.h>
#include "foo.h"
int main() {
printFoo();
return 0;
}
foo.h
#ifndef FOO_H
#define FOO_H
void printFoo();
#endif
foo.c
#include "foo.h"
#include <stdio.h>
void printFoo() {
printf("Foo");
}
But if I place the printFoo() definition in foo.h the debugger runs normally. What causes this problem, and how can I fix it?
I suspect that I need to set up a launch.json (not very familiar with that), but when I click "create a launch.json file" it creates an empty file without any configurations.
I'm using mingw-w64.
Edit:
Here is my tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:/msys64/mingw64/bin/gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/msys64/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

My code cannot open /dev/tty under Linux in VScode debug model

i'm trying to write a simple linux command ‘cp’ from a book Understanding UNIX/LINUX Programming ,but the result it produced is different from debug in vscode.if it run in external console the result is as expected,when it ran in vscode (either ctrl + F5 or Debug) it just
Error: Cannot creat/dev/tty: No such device or address
in 3[1] + Done "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-pagxijfs.jga" 1>"/tmp/Microsoft-MIEngine-Out-kpblt5kt.41t"
What did I do wrong?
here is source code:thank you!🙏
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
#define BUFFERSIZE 4096
#define COPYMODE 0644
void oops(char*,char*);
int main(int ac,char *av[])
{
int in_fd,out_fd,n_chars;
char buf[BUFFERSIZE];
//check args
if(ac!=3)
{
fprintf(stderr,"usage:%s sourcr destination\n",*av);
exit(1);
}
if((in_fd=open(av[1],O_RDONLY))==-1)
{
oops("Cannot open",av[1]);
}
printf("in %d",in_fd);
if((out_fd=open(av[2],O_CREAT|O_WRONLY|O_TRUNC))==-1)
{
//when debug in VScode,it always get here
oops("Cannot creat",av[2]);
}
printf("out %d",out_fd);
while((n_chars = read(in_fd,buf,BUFFERSIZE))>0)
{
if(write(out_fd,buf,n_chars)!=n_chars)
{
oops("Write error to",av[2]);
}
}
if(n_chars==-1)
{
oops("Read error form",av[1]);
}
if(close(in_fd)==-1 || close(out_fd)==-1)
{
oops("Error closing files","");
}
return 0;
}
void oops(char* s1,char* s2)
{
fprintf(stderr,"Error: %s",s1);
perror(s2);
exit(1);
}
here is my config files:
> launch.json ("args": ["cp1.c","/dev/tty"]-----> cp1.c is code 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
"args": ["cp1.c","/dev/tty"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
> task.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": ["-g", "${file}", "-std=c++17", "-o", "${fileBasenameNoExtension}.out"]
}
]
}

How can I solve troubles while debugging c code?

I've been trying the whole day to debug my code using vscode. I set up the lauch.json but It's not working.
I have 3 breakpoints and I should be asked to input 3 floats but when in the Terminal it didn't prompt. Here is my code which it works fine without debugging.
#include <stdio.h>
int main(void)
{
float a, b, c, sum;
printf("Input three floats:");
scanf("%f%f%f", &a, &b,&c); // %f real en coma flotante
printf("a = %f , b = %f, c = %f", a, b,c);
sum = a + b + c;
printf("\nsum = %f\n\n",sum); //%f conversion a punto flotante con signo
return 0;
}
Here is the lauch.json setup
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Iniciar",
"type": "cppdbg",
"request": "launch",
//"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"program": "${workspaceFolder}/sum_3f.exe",
"args": [],
"stopAtEntry": false,
"cwd": "C:\\MinGW\\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Habilitar la impresión con sangría para gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Establecer tipo de desensamblado en Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
I tried to remove "text": "-enable-pretty-printing", cause I read that it could cause trouble but It's still not working.
Here goes 2 images. I'm sorry but I'm not allowed yet to upload images so this is why there are these links:

Launch.json - Wait for first debugger to load, then execute second

I'm pretty new to debugging like this (I'm coming from a .NET background in VS where you pretty much have nothing to worry about).
I'm having an Electron / React setup that I had a pretty hard time to setup in the first place but it works pretty fine now. (From what I read, I'm not alone)
I was fine using the "devTool" for chrome for a while until I required breakpoint and line by line debugging for more complex stuff where it becomes a pain.
I have this so far:
{
"version": "0.2.0",
"configurations": [
{
"name": "Electron: Main",
"type": "node",
"request": "launch",
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"console": "integratedTerminal",
"runtimeExecutable": "npm.cmd",
"runtimeArgs": [
"run-script", "start"
],
"port": 5858
},
{
"name": "Electron: Renderer",
"type": "chrome",
"request": "attach",
"port": 9222,
"webRoot": "${workspaceRoot}",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/electron.cmd"
}
  }
],
"compounds": [
{
"name": "Debug",
"configurations": ["Electron: Main", "Electron: Renderer"]
}
]
}
The two debugger configuration works perfect. If I trigger "Main", then wait for it to open and then trigger the "Renderer" to attach to chrome, it works just fine.
The issue is that I am human. I can't trigger the second one fast enough so If I have stuff I want to debug onload, then I can't.
The "Coumpound" I made is not trigger on time and trigger way too early (I guess when the debugger is attached). It gives me an error message saying it can't attach to said port (which is normal as it hasn't loaded yet.
Question is. How do I make it "wait" for the main to be ready. Is that even doable?
thanks,
Have you had a look at the documentation? Outside of VSCode, it appears you can run the electron command with an --inspect=[port] or --inspect-brk=[port] flag. Then, launch Chrome and visit chrome://inspect and select the launched Electron app.
It appears your launch.json might need to be updated a little bit, here's what that page (on debugging in VSCode) says it should be. I am noticing you are using workspaceRoot instead of workspaceFolder.
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Main Process",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron",
"windows": {
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/electron.cmd"
},
"args" : ["."],
"outputCapture": "std"
}
]
}

Visual Studio Code Hello World in C Debug Function is not working Programm NullReferenceException:

I try to use the Debug Function from Visual Studio Code 1.30.1 on a Ubuntu 18.04.1 as debug extension I use C/C++ 0.20.1 from ms-vscode.cpptools.
I compiled the main.c with gcc -Wall -g main.c -o main
This is the code of the main.c
#include <stdio.h>
int main ()
{
printf("Hello World\n");
}
The launch.json config
{
// 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": [
{
// for Linux
"name": "gdb C",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/a.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
//"preLaunchTask": "build cunit",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
When I start the debuger I get
Stopping due to fatal error: NullReferenceException: Object reference not set to an instance of an object
Visual Studio Code Version:
Version: 1.30.1
Commit: dea8705087adb1b5e5ae1d9123278e178656186a
Date: 2018-12-18T18:07:32.870Z
Electron: 2.0.12
Chrome: 61.0.3163.100
Node.js: 8.9.3
V8: 6.1.534.41
OS: Linux x64 4.15.0-43-generic
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build C",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
//"command":"gcc -g main.c -std=c11 -Werror -Wall -lm"
"command":"gcc -Wall -g main.c"
}
]
}
It looks like in the version I have is a bug in the Software. You can set "externalConsole": false and the debug function is working (this is a workaround).

Resources