Ryan Newman
Ryan Newman

Reputation: 856

Do you need to include header files in a makefile?

bin/queueDriver.o:src/queueDriver.c include/queue.h 
    ${CC} ${CFLAGS} -o bin/queueDriver.o -c src/queueDriver.c

I am learning about Makefiles and I am really confused at this point. I have already written one and now I am messing around and I discovered that if I take

/include/queue.h 

out of my makefile, the project still builds and executes correctly. Why is that?

Upvotes: 2

Views: 5590

Answers (2)

Kerrek SB
Kerrek SB

Reputation: 477710

The names after the colon on the line of the make rule are dependencies. The rule is executed if any of the dependencies have been modified more recently than the target of the rule. (The target is usually a file whose name is the same as the name of the rule, thought there are exceptions.)

If you take any dependencies out, then your target may still build, but it won't be rebuilt even if you change the files it depends on (which long-term may lead to a broken build on a wider scale, e.g. when you attempt to link together two objects built from different versions of a header file).


Finding all the header files that a given source file depends on is typically not humanly possible, so there are a variety of auxiliary tools that one usually uses to auto-generate dependencies. Note that you can add dependencies later:

foo.o: foo.c      ## Manually written rule
        $(CC) -c -o $@ $^

## added later, e.g. by a tool
foo.o: foo.h
foo.o: bar.h

But all in all, make is a very low-level build tool, and it is typically only used either for very small projects or as the backend of some other build system (e.g. autotools or cmake).

Upvotes: 9

Ed Heal
Ed Heal

Reputation: 60047

I guess some people like old technology.

A make file simply saves if a, b, c, if newer than x then build x.

So

x: a, b, c
  do this

is essentially the structure.

So remove the header file is ok as long as you do not modify the header file.

But please consider some other technology such as SCONS, Gradle...

Upvotes: 1

Related Questions