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
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 have the following C application:
#include <stdio.h>
#include <string.h>
int main() {
char name[10];
printf("What is your name?\n");
fflush(stdout);
fscanf(stdin, "%s", name);
printf("Your name is %s.\n", name);
return 0;
}
I am running it with:
Visual Studio Code 1.48.2
C/C++ (Extension for Visual Studio Code) 0.29.0
Code Runner (Extension for Visual Studio Code) 0.11.0
MinGW (mingw32-base) 2013072200
When I hit CTRL+F5 (effectively, Run Without Debugging), I see the prompt, What is your name? in the Debug Console. I try to type my name into the Debug Console, and it gives the following error: Unable to perform this action because the process is running.
How can I read from stdin using the integrated terminal?
Here is my launch.json configuration:
{
"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:\\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"
}
]
}
I was able to get it to take input from stdin using the external console by setting "externalConsole": true, in the above configuration, but I am looking for a way, if possible, to be able to simply write my name in one of the integrated IDE console tabs so that I don't need to launch an external console.
I'm not an English speaker, so I apologize if my writing is strange.
I'm posting this because a similar case occurred to me and I was able to solve it.
I used this as a reference.
(https://github.com/microsoft/vscode-cpptools/issues/5497#issuecomment-628878810)
In my case, the integrated terminal was not accepting keyboard input.
The solution was to change the compiler to this. (https://sourceforge.net/projects/mingw-w64/)
And you probably need to create a path and then rewrite tasks.json and launch.json
Here are my tasks.json and launch.json as an example. Please refer to it.
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe アクティブなファイルのビルド",
"command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "デバッガーによって生成されたタスク。"
}
],
"version": "2.0.0"
}
{
// IntelliSense を使用して利用可能な属性を学べます。
// 既存の属性の説明をホバーして表示します。
// 詳細情報は次を確認してください: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - アクティブ ファイルのビルドとデバッグ",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"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": "gdb の再フォーマットを有効にする",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe アクティブなファイルのビルド"
}
]
}
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 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 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"