Visual Studio Code - debug - c

I'm trying to use the debugger in vs code.
I have problems to activate and run it.
This is my code:
int main(int argc, char const *argv[])}
int arr [] = {54,25,7,2,12,65};
for (int i = 0; i<6; i++)
{
printf("%d ,",arr[i]);
}
printf("\n");
insertion_sort(arr,6);
for (int i = 0; i<6; i++)
{
printf("%d ,",arr[i]);
}
return 0;}
This is my JSON file.
"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": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-win32-dwarf-rt_v6-rev0\\mingw32\\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-win32-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]}
I'm getting this error: Unable to start debugging. program path is missing or invalid.
Thx!

Related

gdb: <error reading variable> in array while debugging VS code

I'm having trouble trying to view the contents of an array during debugging. Instead of characters I see .
My code:
#include <stdio.h>
int main()
{
char str[100] = {0};
fgets(str, 100, stdin);
return 0;
}
What I see in the VARIABLES window:
But when I try to debug int arrays everything works fine.
Exemple:
#include <stdio.h>
int main()
{
int str[100];
for (int i = 0; i < 100; i++)
{
str[i] = i+1;
}
return 0;
}
What I see:
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe\""
}
]
}
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": "${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++: gcc.exe build active file"
}
]
}
What can I do to see the characters in the array?
Because pretty-printer is not displaying utf-8 char correctly. To display utf-8 char, edit the setupCommands in launch.json:
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Fix pretty-printing for gdb",
"text": "set charset UTF-8"
}
],
Original answer: VSCode debugger having issues with character encoding

Visual Studio Code doesn't populate the Problems tab when I try to debug multiple files

HeIIo. This is my first post on Stackoverflow.
I like computer programming and after some experience with Python and VBA I'm trying to learn the C.
Since I took up with it I use Visual Studio Code for training in order to learn how to use modern programming tools at the same time.
I've never had any problem until now, but at the moment I want to compile multiple files.
When I try to compile a file with a warning the terminal shows it
Executing task: C:/mingw-w64/mingw64/bin/gcc.exe -Wall -W -pedantic -ansi -std=c99 -O -g 'd:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackclient.c' 'd:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT3.c' -o 'd:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT.exe'
d:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT3.c: In function 'is_full':
d:\OneDrive\Programmi\C\Kim_N_King-Programmazione_in_C\Capitolo 19\stackADT\stackADT3.c:41:20: warning: unused parameter 's' [-Wunused-parameter]
bool is_full(Stack s) {
~~~~~~^
but in the "Problems" tab it's not shown
(see the screenshot).
In every other folder where there is only a .c file to compile it works flawlessly.
The followings are my jsons file from which you can easily understand my VSC configuration and platform.
Thank you for reading.
c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/mingw-w64/mingw64/bin/gcc.exe",
"cStandard": "c99",
"cppStandard": "c++17",
"intelliSenseMode": "${default}"
}
],
"version": 4
}
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}\\stackADT.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw-w64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "gcc.exe build active file"
}
]
}
tasks.json
{
"tasks": [
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "C:/mingw-w64/mingw64/bin/gcc.exe",
"args": [
"-Wall",
"-W",
"-pedantic",
"-ansi",
"-std=c99",
"-O",
"-g",
"${file}",
"${fileDirname}\\stackADT3.c",
"-o",
"${fileDirname}\\stackADT.exe"
],
"options": {
"cwd": "C:/mingw-w64/mingw64/bin"
}
}
],
"version": "2.0.0"
}
It happens because you compile with flag "-Wall". It means that all warnings will be considered errors. You should use this variable.
for instance
(void*) &s;

I need some help regarding writing C code in vscode in wsl

I downloaded the gcc compiler into my linux environment and used ssh to get into vscode to simulate a linux environment on my windows machine. The files I have created compile and the makefile my professor gave me works fine, however whenever I go to debug using the gdb debugger my professor also provided, which is formatted correctly, the debugger hangs and doesnt allow me to step through my code with break points. I'm providing the code for the debugger as well as the cpp properties and the task files.
debugger code:
{
// 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 build debug stacks",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/runner",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "make stack runner"
}
]
}
cpp properties:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
tasks:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "make stack runner",
"command": "make -f makefile DEBUG=1",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new"
},
"problemMatcher": [
"$gcc"
]
},
]
}
I don't think you require the tasks as cpp_properties. I'm able to use debug perfectly fine on WSL with only the launch.json file (The debugger code you mentioned) which I will give but I'm sure your problem will be solved if
1) Set externalConsole option as false. For some reason, it is not working in windows when set to "true". With this option, your program will run in the integrated console so you can be tricked into thinking that your program is not running if your program has no outputs before the first scanf() or cin command and your breakpoints are after them. So to be sure, have your breakpoints at the first line of your main() function, so that you don't make this classic newbie mistake.
2) Make sure when you compile the program you are using the -g switch. So your compile command should look like
gcc -g path\to\prog.cpp -o a.out
3) Make sure your program is set to the path of this executable file. If this a.out is generated at the root folder of your workspace, then your setting will be
"program":"${workspaceFolder}/a.out"
That should do.
My launch.json setting is
{
// 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": "WSL debugging",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.out",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

VSCode - identifier "size_t" is undefined

I'm writing a code for a simple program that calculates the perimeter of a polygon when its given the amount of sides it has and the length of each side as homework, problem is that when I do for (size_t i = 0; i < sidesnumber; i++) it gives me an error on size_t saying "identifier "size_t" is undefined". I have already tried also including the library stddef.h but it stays the same.
This is the whole code:
#include <stdio.h>
int sidesnumber = 0;
int sides[100];
int results;
main()
{
printf("Number of Sides\n");
sidesnumber = getchar();
sidesnumber -= 48;
for (size_t i = 0; i < sidesnumber; i++)
{
/* code */
getchar();
sides[i] = getchar();
sides[i] -= 48;
}
for (size_t i = 0; i < sidesnumber; i++)
{
/* code */
results += sides[i];
}
printf("The perimeter is:%d", results);
}
I'm using MinGW on Windows 10.
Code of all the .jsons I'm currently using
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile C File",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${workspaceRoot}\\${fileBasename}.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Run C File",
"type": "shell",
"command": ".\\${fileBasename}.exe"
},
{
"type": "shell",
"label": "gcc.exe build active file",
"command": "C:/mingw64/bin/gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/mingw64/bin"
}
}
]
}
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": "${workspaceRoot}\\${fileBasename}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Compile C File"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"browse": {
"path": [
"${workspaceFolder}",
"C:\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include"
],
"limitSymbolsToIncludedHeaders": true
},
"includePath": [
"${workspaceFolder}",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
"C:/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"C:/mingw64/x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"compilerPath": "C:/mingw64/bin/gcc.exe"
},
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
},
{
"name": "Test",
"includePath": [
"${workspaceFolder}/**",
"C:\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++\\tr1",
"C:\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\c++",
"C:\\mingw64\\x86_64-w64-mingw32\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\mingw64\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"compilerArgs": [],
"browse": {
"path": [
"${workspaceFolder}/**"
],
"limitSymbolsToIncludedHeaders": true
}
}
],
"version": 4
}
If you are only concerned about the squiggles under size_t in vscode, you can use std::size_t instead.

The program 'c:\...\a.exe' has exited with code 0 (0x00000000)

I'm new to vscode editor and I want to run simple C project like below, but when I was run this debug console says like below. I want to know how to run this proper way.
Type "apropos word" to search for commands related to "word".
=cmd-param-changed,param="pagination",value="off"
[New Thread 3076.0x2314]
[New Thread 3076.0x20c4]
Thread 1 hit Breakpoint 1, 0x00401603 in main ()
[Thread 3076.0x20c4 exited with code 0]
[Inferior 1 (process 3076) exited normally]
The program 'c:\Users\Lenovo\Desktop\Example\a.exe' has exited with code 0 (0x00000000).
(this is test.c file )
#include <stdio.h>
int main()
{
printf("hellow world");
return 0;
}
I was configured tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "build Hello"
}
]
}
And this is launch.json code I was configured:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/a.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:/MinGW/bin/gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
This is my c_cpp_properties.json file:
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/test.c",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++//tr1",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++/i686-w64-mingw32"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"${workspaceFolder}/test.c",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++",
"C:/MinGW/lib/gcc/i686-w64-mingw32/7.2.0/include/c++//tr1"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
It's just trying to tell you that all went fine. Return value 0 is "no error".
You have a typo in "hello" though, the trailing "w" shouldn't be there.
And main()'s parameters should be either (void) or (int argc, char *argv[]).

Resources