SHREYAS JOSHI
SHREYAS JOSHI

Reputation: 81

Makefile not finding the include file

I Have a folder structure like this.

Gif_Utility

-> Makefile

-> include ( all .h files are over here)

-> Src ( all .c files are over here).

I am writing a makefile.

Makefile

VPATH = src:include
INC = -I  ./include

gif_objects = gif_display.o \
           gif_lzw.o \
           gif_read.o \
           sysm.o \
           x86_main.o


gif_display.0 : gif_display.c  
    gcc -I /export/home/joshis1/MakeTutorial/GIF_Utility/include -c $<

#gif_lzw.0 : gif_lzw.c
#   gcc $(INC) -c src/gif_lzw.c

#gif_read.0 : gif_read.c 
#   gcc -I ./include/ -c $< 

#sysm_main.0 : sysm_main.c 
#   gcc -I ./include/ -c $< 

#x86_main.0 : x86_main.c 
#   gcc -I ./include/ -c $< 


On command prompt:
$ make gif_display.o

cc    -c -o gif_display.o src/gif_display.c
src/gif_display.c:2:17: fatal error: sysm.h: No such file or directory
compilation terminated.
make: *** [gif_display.o] Error 1

On the other hand, If i do like this it compiles fine $make -> this creates the gif_display.o

I don't know why it is throwing error on specifying the rule. Am I missing something, please help.

I am using Ubuntu machine to build my code.

Upvotes: 1

Views: 4393

Answers (1)

tripleee
tripleee

Reputation: 189297

Your makefile has a .0 (zero) suffix instead of the correct .o (letter oh).

When you run just make it attempts to build the first target in the Makefile, and so runs the recipe which contains the correct -I include path. The fact that it produces an .o file, not a .0 file, is acceptable to make, although I suppose it could at least print a warning in this scenario.

When you run make gif_display.o it uses Make's built-in productions for .o files, which do not contain your include path.

Given that make already has built-in rules for .o files, your rules are basically superfluous. It would be better to just add the -I include parameter to the default rules, with something like

CFLAGS += -I include

So the entire Makefile could be as simple as this:

VPATH = src:include
CFLAGS += -I  ./include

gif_objects = gif_display.o \
           gif_lzw.o \
           gif_read.o \
           sysm.o \
           x86_main.o

I don't see anything to alert your Makefile to changes in .h files, so you might want to add a separate dependency for that. If you just have a few shared header files between all your object files, maybe this will be all you need:

$(gif_objects): $(wildcard include/*.h)

This basically makes the include component of the VPATH superfluous.

Upvotes: 2

Related Questions