0

I'm doing a more complex project in c, but this example here is the same for what i need and the names of examples will just changed, but, again, is the same thing from what i am doing. Let's say I have this file structure:

|--project/
|--main.c
|--modules/
|   |--include/
|      |--module1.h
|      |--module2.h
|      |--module3.h
|   |--impl/
|      |--module1.c
|      |--module2.c
|      |--module3.c

Module2 uses functions I created and the implementated of my module1. Therefore, there is a dependence of module2 on module1. In other words, my module2 calls module1.

module1.h

#ifndef MODUL1_H
#define MODUL1_H

void functionModule1();

#endif

module2.h

#ifndef MODUL2_H
#define MODUL2_H

void functionModule2();

#endif

module3.h

#ifndef MODUL3_H
#define MODUL3_H

void functionModule3();

#endif

module1.c

#include "module1.h"
#include <stdio.h>

void functionModule1() {
    (...)
}

module2.c

#include "module2.h"
#include "module1.h"
#include <stdio.h>

void functionModule2() {
    functionModule1();
    (...)
}

module3.c

#include "module3.h"
#include "module2.h"
#include "module1.h"
#include <stdio.h>

void functionModule3() {
    functionModule2();
    (...)
}

main.c

#include "module3.h"
#include <stdio.h>

int main() {
    functionModule2();
    (...)
    return 0;
}

I am compiler the files in this way: gcc main.c module/impl/* -o a.out

But i receive an error:

fatal error: module1.h: Nonexistent file or directory
    1 | #include "module1.h"
      |          ^~~~~~~~~~~
compilation terminated.

fatal error: module2.h: Nonexistent file or directory
    1 | #include "module2.h"
      |          ^~~~~~~~~~~
compilation terminated.

fatal error: module3.h: Nonexistent file or directory
    1 | #include "module3.h"
      |          ^~~~~~~~~~~
compilation terminated.

What am i doing wrong?

I tried what I explained above;

7
  • 2
    Add -Imodules/include to your compiler flags so it knows where to look for the headers. Commented Nov 30, 2023 at 0:40
  • @Shawn gcc -Imodules/include main.c module/impl/* -o a.out like this? Commented Nov 30, 2023 at 1:06
  • @TomKarzes Is just an example. I believe this does not affect the error. In my project the functions have arguments. Commented Nov 30, 2023 at 1:07
  • Suggestion: Sequence the #include statements from general to specific, not the reverse as you've done. Eg: <stdio.h> is generally useful (to many C programs). In the future you may wish to write a macro that, for instance, uses fundamental functions like printf()... Start with OS specific headers, if required, then language headers, then finally the application specific headers... As a rule-of-thumb, this practice will help you make progress more quickly. Commented Nov 30, 2023 at 3:49
  • Use a decent programming IDE and you won't run into problems like this. Manually appending -I etc to the compiler was how we programmed in the stone age (or well, 1990s). Don't waste time on irrelevant crap like setting up paths and creating make files - this is what an IDE exists for. If your IDE still requires you to manually pass along -I (like Eclipse) then get a better IDE that is actually useful for programming. Commented Nov 30, 2023 at 8:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.