I have two questions. I have just installed VS code and managed to make it compile C codes and show outputs. But I am not able to debug. When I add a breakpoint and Debug, Red circle greys out.
I read on github that adding a -g flag will work.
Q1. But where and how to add -g flag? I also read:
How to add compile flag -g to a make file?
But it passed over my head.
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}/try.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
"preLaunchTask": "echo",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0\\include\\c++"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "gcc",
"args": [
"-Wall", "try.c", "-o", "try"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I tried to change args in tasks.json from -o to -g, but it also stopped it from compiling, which was at least working before. If I added besides -o, program still didn't debug.
Edit:
Q2. Plus also telll me if it's ok to add path of C++ in tasks.json here instead of C?
Because I couldn't find path for C. Tutorials on internet were for C++ and they told to set C++ path there. But I want to compile C codes and although they are compiling now.
"includePath": [
"${workspaceFolder}/**",
"C:\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0\\include\\c++"
You should add the flag, not replace the "-o" flag.
The -o flag tells the compiler the name of the output file.
So instead have e.g.
"-Wall", "-g", "try.c", "-o", "try"
Related
I'm trying to link the math library to the C debugger than comes with visual studio code, GCC. I'm want to mimic what the argument -lm does when I use a makefile. To my understanding I can do this using the launch.json file shown below.
{
"version": "0.2.0",
"configurations": [
{
"name": "gcc - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": ["nums"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
I looked through the documentation https://code.visualstudio.com/docs/cpp/launch-json-reference. The closest thing I could find was "miDebuggerArgs". I tried adding the line "miDebuggerArgs": "-lm" to the "configurations" list, but it didn't work. Any advice?
You posted the launch.json file. Go to the tasks.json and add -lm to the arguments.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/bin/${fileBasenameNoExtension}",
"-lm"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"isDefault": true,
"kind": "build"
},
"detail": "compiler: /usr/bin/gcc"
}
]
}
I'm getting the following fatal error while trying to include glib.h into a project. mingw gcc cant find glib.h.
fatal error: glib.h: No such file or directory
I've isolated the issue in a simple helloworld.c file.
Upon Ctrl-Shift-B to build, I get this.
I've also updated my includePath in my c_cpp_properties.json file per the instructions I read here.
Does anyone have any ideas of what I should look into or do to resolve this? I have spent many hours but not made any progress on this. :(
helloworld.c is:
#include <stdio.h>
#include <glib.h>
void main() {
printf("Hello World!");
char c = getchar();
}
c_cpp_properties.json is:
{
"configurations": [
{
"name": "MingGW",
"includePath": [
"${workspaceFolder}/**",
"C:\\cryptolibs\\msys2-64\\mingw64\\include\\glib-2.0",
"C:\\cryptolibs\\msys2-64\\mingw64\\lib\\glib-2.0\\include",
"C:\\cryptolibs\\pbc-0.5.14\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:\\MinGW\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"${workspaceFolder}",
"C:\\cryptolibs\\msys2-64\\mingw64\\include\\glib-2.0",
"C:\\cryptolibs\\msys2-64\\mingw64\\lib\\glib-2.0\\include",
"C:\\cryptolibs\\pbc-0.5.14\\include"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
launch.json 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": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.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
}
],
"preLaunchTask": ""
}
]
}
tasks.json is:
{
"tasks": [
{
"type": "shell",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\MinGW\\bin\\gcc.exe",
"args": ["-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe"],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
To be able to use glib.h you need to have glib2 on your system.
If you have a shell (like MSYS2) normally you can get the compiler flags with:
pkg-config --cflags glib-2.0
and the linker flags with:
pkg-config --libs glib-2.0
On my MSYS2 system for example this returns:
-LD:/Prog/winlibs64-10.2.0/custombuilt/lib -lglib-2.0 -lintl
and
-mms-bitfields -ID:/Prog/winlibs64-10.2.0/custombuilt/include/glib-2.0 -ID:/Prog/winlibs64-10.2.0/custombuilt/lib/glib-2.0/include -ID:/Prog/winlibs64-10.2.0/custombuilt/include
respectively.
I recently switched to the Ubuntu 16.04. I am using vscode for as IDE on Ubuntu. I configure other languages, but I could not do it for C/C++. I created c_cpp_properties.json, launch.json & tasks.json. When I started to compile the any given code, It gives an error when the fit functions like printf or malloc.
The Error message:
Unable to open 'printf.c': File not found (file:///build/glibc-Cl5G7W/glibc-2.23/stdio-common/printf.c).
How can I fix the problem?
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}/test.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"pointer_revision.c",
"-o",
"test.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json
{
"configurations": {
"name": "Linux",
"includePath": [
"${workspaceFolder}",
"/usr/include/x86_64-linux-gnu/5/include",
"/usr/local/include",
"/usr/include/x86_64-linux-gnu/5/include-fixed",
"/usr/include/x86_64-linux-gnu",
"/usr/include"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"${workspaceFolder}",
"/usr/include/x86_64-linux-gnu/5/include",
"/usr/local/include",
"/usr/include/x86_64-linux-gnu/5/include-fixed",
"/usr/include/x86_64-linux-gnu",
"/usr/include"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"compilerPath": "/usr/bin/gcc"
}
}
Debian and Ubuntu do not ship sources as part of the debugging packages unfortunately. As far I can tell, there is no plan to ship sources as part of dbgsym packages, either.
In contrast, Fedora and its downstream distributions have extensive infrastructure to prepare usable source files for debugging. It is not entirely trivial to do this because it requires rewriting file paths in the DWARF data, from the build tree location to the installation location. But it can be really helpful for debugging and gives a nice free software flavor to the entire distribution.
I'm trying to debug a C program using Visual Studio Code on Windows 10,
which I have the C/C++ extension installed in.
My problem is that when I create Source.c in my workspace ( E:\Docs\c ), write some code then hit F5, it shows an error message launch: program 'E:\Docs\c\a.exe' does not exist, which means VSCode doesn't do the compiling thing.
Meanwhile when I go to the console and type gcc source.c, which creates a.exe in the same folder, and hit F5 again it starts with no problems, but doing that every time I want to run the code is annoying.
So, is there a way to compile the code from inside VSCode ?
Here is my c_cpp_properties.json :
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}",
"C:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\lib\\gcc\\i686-w64-mingw32\\8.1.0\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
And this is launch.json :
{
"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:\\Program Files (x86)\\mingw-w64\\i686-8.1.0-posix-dwarf-rt_v6-rev0\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
I think you should add prelaunched task with label of your build task to launch.json like this:
"preLaunchTask": "build" // label of your build task
This means you should have in your tasks.json following task with label build e.g.
"tasks": [
{
"label": "build",
"type": "shell",
"command": "gcc -g source.c"
"group": {
"kind": "build",
"isDefault": true
}
}
]
Also "-g" flag is important for enabling debugging
I recently switched to the Ubuntu 16.04. I am using vscode for as IDE on Ubuntu. I configure other languages, but I could not do it for C/C++. I created c_cpp_properties.json, launch.json & tasks.json. When I started to compile the any given code, It gives an error when the fit functions like printf or malloc.
The Error message:
Unable to open 'printf.c': File not found (file:///build/glibc-Cl5G7W/glibc-2.23/stdio-common/printf.c).
How can I fix the problem?
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}/test.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"pointer_revision.c",
"-o",
"test.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json
{
"configurations": {
"name": "Linux",
"includePath": [
"${workspaceFolder}",
"/usr/include/x86_64-linux-gnu/5/include",
"/usr/local/include",
"/usr/include/x86_64-linux-gnu/5/include-fixed",
"/usr/include/x86_64-linux-gnu",
"/usr/include"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"${workspaceFolder}",
"/usr/include/x86_64-linux-gnu/5/include",
"/usr/local/include",
"/usr/include/x86_64-linux-gnu/5/include-fixed",
"/usr/include/x86_64-linux-gnu",
"/usr/include"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"compilerPath": "/usr/bin/gcc"
}
}
Debian and Ubuntu do not ship sources as part of the debugging packages unfortunately. As far I can tell, there is no plan to ship sources as part of dbgsym packages, either.
In contrast, Fedora and its downstream distributions have extensive infrastructure to prepare usable source files for debugging. It is not entirely trivial to do this because it requires rewriting file paths in the DWARF data, from the build tree location to the installation location. But it can be really helpful for debugging and gives a nice free software flavor to the entire distribution.