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.
Related
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!
What I'm trying to do:
I need to write integers into a 2d array. The length of the array is N*N. So I scanf to get the value of N from the user.
The C/C++ extension gives the "expression must have a constant value". But building it with gcc works perfectly fine
What I've tried:
C/C++ extension gives error on N in both arrays, "expression must have a constant value".
After some googling answers, I tried to set my compiler version in the extension to c99, since that is the version which supports variable length arrays. But it still give the same error. Tried using other newer c versions, the intellicense still gives the same error.
Code and settings:
tree.c:
#include <stdio.h>
#include <stdlib.h>
int main() {
int N, i, j;
scanf("%d", &N);
int min_tree[N][N];
int tree_walked[N];
}
c_cpp_properties.json:
{
"name": "TUF_Laptop",
"includePath": [
"${workspaceFolder}/**",
"/usr/local/include",
"/usr/include",
"/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include",
"/usr/lib/gcc/x86_64-pc-linux-gnu/11.1.0/include-fixed",
"/usr/lib/gcc/x86_64-pc-linux-gnu"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "/usr/bin/gcc",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
"-O3 -Wall -Wextra -std=c99"
],
"cStandard": "c99",
"cppStandard": "gnu++17",
"compileCommands": ""
}
Similar answers I found:
"expression must have a constant value" error in VS code for C
The IDE may be warning you of a potential programming error, since value N can be changed while mintree and treewalked are still in scope. You should be able to fix this with the following modification:
#include <stdio.h>
#include <stdlib.h>
int main() {
int N_scan, i, j;
scanf("%d", &N_scan);
const int N = N_scan;
int min_tree[N][N];
int tree_walked[N];
}
The value of N cannot be changed while min_tree and tree_walked are in scope.
There is a similar answer (for a C++ case) here:
https://stackoverflow.com/questions/9219712/c-array-expression-must-have-a-constant-value#:~:text=expression%20must%20have%20a%20constant%20value.%20When%20creating,declared%20const%3A%20doesn%27t%20even%20provide%20a%20variable%20name.
Just to add to the other answer, VS Code's intellisense defaults to MSVC as compiler. MSVC does not support the entirety of C99 including non-constant length arrays.
You can change the compiler used in VS Code's settings and set it to GCC which supports this feature.
In vs code, type "C/C++: Edit Configurations (UI)" (without the quotes) in the command palette (Ctrl + Shift + P) and edit the configuration. It will however create a .vscode/c_cpp_properties.json file in your folder. I don't think there is a way to set it globally other than copying this file to every .vscode folder.
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.
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)
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"
}
}