Makefile include header

c, makefile

Solution

You must link against the compilation unit `inc.o` which you obtain by compiling `inc.c`.

In general that means that you must supply all object files that contain functions that are used in `main.c` (transitively). You can compile these with implicit rules of `make`, no need to specify extra rules.

You could say:

app: main.c inc.o inc.h
    cc -o app inc.o main.c

And `make` will know on its own how to compile `inc.o` from `inc.c` although it will not take `inc.h` into account when determining whether `inc.o` must be rebuilt. For that you would have to specify your own rules.

Problem

I am new to Linux programming I tried to compile a simple test construction. But I'm getting an error when compiling. Adding inc.c as well (in the app: line) doesn't work. How should I include the file correct? Makefile: ``` app: main.c inc.h cc -o app main.c ``` Terminal: ``` make cc -o app main.c /tmp/ccGgdRNy.o: In function `main': main.c:(.text+0x14): undefined reference to `test' collect2: error: ld returned 1 exit status make: *** [app] Error 1 ``` main.c: ``` #include <stdio.h> #include "inc.h" int main() { printf("Kijken of deze **** werkt:\n"); test(); getchar(); return 0; } ``` inc.h ``` #ifndef INCLUDE_H #define INCLUDE_H void test(); #endif ``` inc.c ``` #include <stdio.h> void test() { printf("Blijkbaar wel!"); } ```

Original source