How to fix C header #include errors in VSCode on Mac? - c

I am using VSCode to run multiple C files, but VSCode doesn't seem to read the header files properly:
My hello.c file in project/src/hello.c file looks like this
#include <stdio.h>
#include "hellofunc.h"
int main(int argc, char* argv[]) {
//body
}
The referenced hellofunc.h file is in project/include/hellofunc.h
However, when I run hello.c in VSCode, I get the following error:
"[PATH]/project/src/"hello
hello.c:2:10: fatal error: 'hellofunc.h' file not found
My c_cpp_properties.json file looks like this:
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "macos-clang-x64",
"configurationProvider": "ms-vscode.makefile-tools"
}
],
"version": 4
}
I know I need to add the file path to the project/include folder somewhere, but I haven't been able to figure out where. Can someone help? Thanks!

Related

problem including gtk/gtk.h file not found windows 10 Visual Studio Code

There seems to be a problem floating around for some people using VSCode. I have confirmed the installation of GTK with the terminal window and everything went well.
I wrote a quick program in C to read an address and print it back. It compiles and runs (without the "#include <gtk.gtk.h">) creating the .exe file so I can confirm VScode is installed correctly.
#include <stdio.h>
#include <gtk/gtk.h>
int main()
{
int var = 20; /* variable declaration*/
int *ip; /* pointer variable declaration */
ip = &var; /* store the address of var in pointer variable */
printf("Address of var variable: %x\n", &var);
/* address stored in the pointer variable */
printf("Address stored in ip the pointer variable: %x\n", ip);
/* access the variable the *ip variable points to */
printf("Value of *ip variable: %d\n", *ip);
return 0;
}
When I add the #include <gtk/gtk.h> statement, I get the error that it cannot be found. >#include errors detected. Please update your includePath.<
I used the command "pkg-config --cflags --libs gtk+-3.0" in a terminal to create the path in c-cpp.json file
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/msys64/mingw64/bin",
"C:/msys64/mingw64/include/gtk-3.0",
"C:/msys64/mingw64/include/pango-1.0",
"C:/msys64/mingw64/include",
"C:/msys64/mingw64/include/glib-2.0",
"C:/msys64/mingw64/lib/glib-2.0/include",
"C:/msys64/mingw64/include/harfbuzz",
"C:/msys64/mingw64/include/freetype2",
"C:/msys64/mingw64/include/libpng16",
"C:/msys64/mingw64/include/fribidi",
"C:/msys64/mingw64/include/cairo",
"C:/msys64/mingw64/include/lzo",
"C:/msys64/mingw64/include/pixman-1",
"C:/msys64/mingw64/include/gdk-pixbuf-2.0",
"C:/msys64/mingw64/include/atk-1.0",
"C:/msys64/mingw64/lib"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/msys64/mingw64/bin/gcc.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
the directory gtk is in the C:/msys64/mingw64/include/gtk-3.0 directory and the gtk.h file is in the gtk directory. Intellisense even prompted for the directory and file.

Cannot build while I include own header file

I want to use C on Linux - VSCode. Therefore, I referred tutorial Using C++ on Linux in VS Code to use C/C++ extension, and accomplished to build hello world. Next, I want to test "include .h" by adding adder.h/.c:
//adder.h
#include <stdio.h>
int add(int a, int b);
//adder.c
#include "adder.h"
int add(int a, int b)
{
return a + b;
}
//main.c
#include <stdio.h>
#include "adder.h"
int main()
{
printf("ret = %d\n", add(1,2));
}
Those code can be build on DevC++ without any other setting. However, it showed error message after Terminal -> Run Build Task...
:
> Executing task: C/C++: gcc build active file <
Starting build...
/usr/bin/gcc -g /home/hughesyang/Test/c/projects/multi_files/main.c -o /home/hughesyang/Test/c/projects/multi_files/main
/tmp/ccvaDYKq.o: In function `main':
/home/hughesyang/Test/c/projects/multi_files/main.c:6: undefined reference to `add'
collect2: error: ld returned 1 exit status
Build finished with error(s).
The terminal process failed to launch (exit code: -1).
I'm not sure whether I need to modify DEFAULT tasks.json to solve that? Or it's caused by other mistake?
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: /usr/bin/gcc"
}
]
}
PS. Using C++ on Linux in VS Code is a C++ example. Thus, I replace g++ with gcc as compiler for C.
Thanks for #WhozCraig's hint. It needs to use cmake to accomplish that. After referring Get started with CMake Tools on Linux and adding adder.c inside the add_executable() of CMakeLists.txt, it works!
cmake_minimum_required(VERSION 3.0.0)
project(helloworld_cmake VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(helloworld_cmake main.c adder.c)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

VSC C Include errors detected

I keep getting the errors #include errors detected. Please update your includePath. IntelliSense features for this translation unit (C:\Users\USERNAME\Documents\GIT\CS-262-004\HelloWorld.c) will be provided by the Tag Parser.C/C++(1696) and cannot open source file "stdio.h"
from the include statement I have tried Reinstalling the GCC from MINGW and I have added the file to the Path, I am using VSC so i added the C/C++ extension by microsoft and Code Runner by Jun han.
edit: added a semicolon after the printf statement and errors weren't fixed
My Code:
FileName - HelloWorld.c
#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
.vscode files
c_cpp_proporties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "gcc-x64",
"compilerPath": "C:/MinGW/bin/gcc.exe"
}
],
"version": 4
settings.json
{
"files.associations": {
"HelloWorld.c": "c"
}
}

Visual Studio Code Include path error in C

It seems to be a simple problem but I can't find a solution to it.
I'm having a issues while including the header of a basic function I created.
main.c:2:10: fatal error: 'add.h' file not found
#include "add.h"
I made this code as simple a possible to highlight the main issue.
Here's my main code:
#include<stdio.h>
#include "add.h" //add.c function header
int main(int argc, char const *argv[])
{
int num1 = 5, num2 = 6;
add(num1, num2);
return 0;
}
add.c function code:
#include <stdio.h>
void add(int n1, int n2)
{
int sum = n1 + n2;
printf("%d + %d = %d\n", n1, n2, sum);
}
And the add.h
void add(int n1, int n2);
My c_cpp_properties.json file.
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/header"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
Project structuration:
Test_Header(folder)
source(folder)
main.c
add.c
header(folder)
add.h
I'm on Mac, my editor is Visual studio Code and I run the program with the run button provided by the "code runner" visual studio code extension.
I have a little light bubble on the top of my #include "add.h" saying "add to include path".
My guess is that it comes from the compiler, like he's not looking at the right place for the headers.
How can I change that (if that's what I need to do)?
Also, do changing it wouldn't change where he's supposed to find the built in c function library , like stdio ?
Or he can understand that he has a folder for the standard c function library and an other for user custom headers that should be located in the project directory ?
I've been 3 days on that issue, Couldn't find an answer on the web, or stuff that I couldn't understand. I could switch on an IDE but it will be like giving up.
I'm fairly new in the world of programmation as you can guess, so if you can keep it as simple as possible, it would be great :).
Thank you.

Unable to debug a single .c file in vscode

I think it should be 6 hours I`m dealing with this problem! Anyway, I want to build a single .c file in vscode in linux ubuntu 64bit. Here is my code(quadratic-equation.c):
#include <stdio.h>
#include <math.h>
double quadratic_equation(float a, float b, float c);
int main()
{
float x, a, b, c;
printf("Enter a: ");
scanf("%f", &a);
printf("Enter b: ");
scanf("%f", &b);
printf("Enter c: ");
scanf("%f", &c);
x = quadratic_equation(a, b, c);
printf("form: ax%c + bx + c = 0:\n", 253);
printf("x = %f", x);
return 0;
}
double quadratic_equation(float a, float b, float c)
{
float x;
if(a == 0)
{
if(b == 0)
{
printf("a & b can not be both zero");
}
else
{
x = -c / b;
return x;
}
}
if((b * b - 4 * a * c ) == 0)
{
x = -b / (2 * a);
return x;
}
x = (-b + sqrt(b * b - 4 * a * c)) / 2 * a;
return x;
}
I have resolved all the problems in this code.
On the way to build, VSCode forced me to create 3 files, c_cpp_properties.json, launch.json, And tasks.json.
So, I copy paste them here:
My c_cpp_properties.json file:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
My launch.json 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}/quadratic-equation.c",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build quadratic equation"
}
]
}
My tasks.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build quadratic equation",
"type": "shell",
"command": "g++",
"args": [
"-g", "quadratic-equation.c"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
And here is what gdb --version outputs:
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
I also tried adding makefile file which didnt help.(I actually didnt know how to configure it, But I searched a little and had some tries, Which didn`t help).
And finally, Here is the error I get when I press f5 (start debugging):
Unable to start debugging. Program path '/home/amirali/XPSC/test/c-cpp/quadratic-equation.c' is missing or invalid.
GDB failed with message: "/home/amirali/XPSC/test/c-cpp/quadratic-equation.c": not in executable format: File format not recognized
This may occur if the process's executable was changed after the process was started, such as when installing an update. Try re-launching the application or restarting the machine.
Really thanks for your help
Finally found the answer.
Run gcc -g -o {executableDesiredFileName} {yourFile}.c -lm in the working folder and path "program" in launch.json to the executable file (-lm will try to include hadditional headers, and -o will rename the compiled program as the default is a.out).

Resources