Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I `include` custom headers which is inside `includes/` inside `functions/create_note.c`

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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!

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading