Makefile?

Thread Starter

Mathematics!

Joined Jul 21, 2008
1,036
I am learning makefiles and I am wondering about a few things

I have been looking at the example

Rich (BB code):
objects = main.o kbd.o command.o display.o \
          insert.o search.o files.o utils.o

edit : $(objects)
        cc -o edit $(objects)
main.o : main.c defs.h
        cc -c main.c
kbd.o : kbd.c defs.h command.h
        cc -c kbd.c
command.o : command.c defs.h command.h
        cc -c command.c
display.o : display.c defs.h buffer.h
        cc -c display.c
insert.o : insert.c defs.h buffer.h
        cc -c insert.c
search.o : search.c defs.h buffer.h
        cc -c search.c
files.o : files.c defs.h buffer.h command.h
        cc -c files.c
utils.o : utils.c defs.h
        cc -c utils.c
clean :
        rm edit $(objects)

which can be simplified to
Rich (BB code):
objects = main.o kbd.o command.o display.o \
          insert.o search.o files.o utils.o

edit : $(objects)
        cc -o edit $(objects)

main.o : defs.h
kbd.o : defs.h command.h
command.o : defs.h command.h
display.o : defs.h buffer.h
insert.o : defs.h buffer.h
search.o : defs.h buffer.h
files.o : defs.h buffer.h command.h
utils.o : defs.h

.PHONY : clean
clean :
        -rm edit $(objects)
Because of some implicit rule

But I am going to have tons of .c files in this directory that I am making
So it would take for ever to write a line out for each .o files as a target with specific dependencies like the above 2 examples do. Even if the implicit rule allows me to leave off the cc -c condition lines

I thought of this but won't this below simple version recompile all the .c files if any of the .o files have changed. For get for the moment about the .h files

Rich (BB code):
#would this work?
SRCS = start32.c kmain.c 
OBJS = $(SRCS:.c=.o )

$(OBJS) : $(SRCS)

Rich (BB code):
$(OBJS) : $(SRCS)
is equivalent to
start32.o kmain.o : start32.c kmain.c 
            cc -c start32.c kmain.c 

I believe CORRECT ME IF I AM WRONG
so I would think that everytime start32.c change or kmain.c changed it would recompile both when what I want to do is just have start32.c being recompiled when start32.c is modified and kmain being compiled only when kmain.c changes NOT BOTH.

And I don't want to specify each one by its target:dependencies since I am going to have a ton of c files in this directory... so the 2 first makefile examples are not an option.
Is it even possible or must you manual tell each .o file which .c files it depends on or should recompile.
Now that I can just add to the SRCS variable in the makefile and OBJS will contain what sources has in it replaced with a .o extension for .c
Will I still have to have a target : dependency for each object file defeating the whole purpose of me just wanting to compile and newly add or changed files with the minimum dependencies. ( AND ALSO DEFEATING THE PURPOSE OF THE SRCS and OBJS variables )
Like if I add whatever.c to the SRCS var and then issue make I just want it to compile whatever.c not all the other .c files.
Obviously you would still have to do the linking target process with all of them again if one .o file was changed but that is obvious.
 
Last edited:
Top