So this is my source tree for a simple note-taking app I'm creating.
Folder PATH listing for volume Seagate Ext
Volume serial number is 6C91-D326
E:.
ª .gitignore
ª README.md
ª
+---.vscode
ª c_cpp_properties.json
ª settings.json
ª
+---src
ª compile.sh
ª main.c
ª note-taker.config
ª
+---functions
ª create_note.c
ª delete_note.c
ª view_note.c
ª
+---includes
foo.c
foo.h
func.h
Whenever I run gcc main.c includes/foo.c functions/create_note.c functions/view_note.c functions/delete_note.c -o x
I'm getting:
functions/create_note.c:7:10: fatal error: includes/foo.h: No such file or directory
#include "includes/foo.h"
^~~~~~~~~~~~~~~~
compilation terminated.
functions/view_note.c:3:10: fatal error: includes/func.h: No such file or directory
#include "includes/func.h"
^~~~~~~~~~~~~~~~~
compilation terminated.
functions/delete_note.c:3:10: fatal error: includes/func.h: No such file or directory
#include "includes/func.h"
^~~~~~~~~~~~~~~~~
compilation terminated.
Inside my create_note.c
this is how I've ordered the preprocessors.
#include <io.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "includes/foo.h"
#include "includes/func.h"
func.h
has three functions declared which are all inside functions/
, and create_note.c
is one of them. The above shows how the preprocessors are inside create_note.c
as mentioned. How would I solve the compile error?
I've tried doing this in my #include
:
#include "src\include\foo.h"
#include "./includes/foo.h
Expectation is: the program should compile successfully and work!
You have two options to solve this.
OPTION 1:
assuming you current working directory is src
add the -I includes/
flag to gcc
this will tell gcc
to look for headers in the includes/
directory
then you will have to include like this
#include "func.h"
instead of this #include "includes/func.h"
if you want to include it like #include "includes/func.h"
add -I .
flag instead
Here is how the full command would be
gcc main.c includes/foo.c functions/create_note.c functions/view_note.c functions/delete_note.c -I . -o x
OPTIONS 2:
prefix your include headers with ../
..
means previous dir so to include includes/func.h
from functions/create_note.c
include this way
#include "../includes/func.h"
WHY YOUR CODE DIDN'T GIVE YOU THE EXPECTED RESULT:
when you type #include "src\include\foo.h"
the compiler actually search for the header file in E:\src\functions\src\include
which doesn't exist
it searches for it relatively to create_note.c
#include "./includes/foo.h"
didn't work for the same reason the only diffrence it was looking in a different directory in this case it was E:\src\functions\includes
.