unable to open printf, unable to read file [duplicate] - c

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.

Related

Issue with launching C program in VS Code. Probably something with the debugger

So, let me give a bit of an introduction first. I'm a 1st semester CS student and have C as a subject. My college recommends DevC++ but it doesn't run properly in my system so I tried using VS Code. My issue is that, in case of the program below, the output window pops up and then closes by itself. I have attached my code, launch.json, tasks.json, c_cpp_properties.json and the debug log as well below. Adding targetarchitecture in launch.json doesn't help.
I have also posted the same issue in github but while commenting my account was somehow flagged and so far, there haven't been any replies.
My code:
#include<stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
My launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"targetArchitecture": "x86_64",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"internalConsoleOptions": "openOnFirstSessionStart",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MingGW2\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"logging": {
"moduleLoad": true,
"programOutput": true,
"trace": true,
"traceResponse": true,
"engineLogging": true
},
},
]
}
My tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc build & run active file",
"command": "C:\\MingGW2\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\MingGW2\\bin"
},
"problemMatcher":"$gcc",
"group": {
"kind": "build",
"isDefault": true
}
}
],
}
My c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\MingGW2\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x86"
}
],
"version": 4
}
The debug log is big and I'm quite sure it's repeating but, as I can't figure out any of it, I've included the whole log below.
The debug log

Get No such file or directory in Visual Studio Code when Debug with gcc

My C project need use MySQL, so there is code:
#include "mysql.h"
I just couldn't find out where should I add include path, or which key point I missed, I think I have get it done already. When I debug with (Windows)启动, it's works fine. But when I choose "gcc.exe", it always get "mysql.h: no such file or directory", please help!
I tried everything I can but still get this error.
this is my c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Program Files/MySQL/MySQL Server 8.0/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/Program Files/mingw64/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
this is my launch.json:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Program Files\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
},
{
....................
},
]
}
this is my task.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\Program Files\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"${file}",
"-I",
"C:/Program Files/MySQL/MySQL Server 8.0/include",
"-L",
"C:/Program Files/MySQL/MySQL Server 8.0/lib",
"-llibmysql",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\Program Files\\mingw64\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: \"C:\\Program Files\\mingw64\\bin\\gcc.exe\""
}
]
}
The first thing I would try is hiding those spaces in your paths; terminal will read them as the end of an argument and start of a new one.
For example, in your launch.json, you have
"options": {
"cwd": "C:\\Program Files\\mingw64\\bin"
},
which I would try editing to
"options": {
"cwd": "C:\\\"Program Files\"\\mingw64\\bin"
},
Currently, terminal sees the path "C:\Program" and interprets "Files\mingw64\bin" as some weird argument that it probably just ignores. Your windows debugger may be smart enough to figure out that's wrong, but terminal operates pretty literally.
With "Program Files" contained in quotes (with escape chars), terminal should be able to read the whole path. Try that everywhere you have spaces in your paths and see if it helps.

Debugging C using Visual Studio Code on Windows

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

Unable find printf.c on Ubuntu 16.04

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.

Where to add -g flag in vs code for debugging

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"

Resources