Chris
Chris

Reputation: 335

makefile - alway recompile version.c when recompiling other c files

I want to recompile version.c whenever an other c-file has changed. So i tried to touch the c-file whenever an other c-file has changed. The Problem is that make doesn't recognize the change when it is allready doing the c-compiling. I think the reason is that it has already updated all dependencies. Is there a way to break the chains?

For example:

$(TARGET): $(OBJECTFILES) $(TARGET).a

$(OBJECTDIR)/%.o: %.c | $(OBJECTDIR)
  $(if $(filter $(VERSION_BUILD),0),$(shell touch $(PROJECT_ROOT_PFAD)/$(VERSION_FILE)))

  #When VERSION_FILE then VERSION_BUILD is set to 1
  $(CC).....

So i want make to recognize the updated file immediately.

Upvotes: 0

Views: 53

Answers (2)

Renaud Pacalet
Renaud Pacalet

Reputation: 29025

I want to recompile version.c whenever an other c-file has changed.

So, you must declare that the result of the compilation of version.c depends on the other C source files. The following computes the list of the other C source files and assigns it to a make variable:

OTHER_C_SOURCES := $(filter-out version.c,$(wildcard *.c))

The result of the compilation of version.c is version.o. The rule you need is thus:

version.o: version.c $(OTHER_C_SOURCES)
    $(CC) -c $(CFLAGS) $< -o $@

That's it. It says: if version.c or any of the other source files change, rebuild version.o by executing:

$(CC) -c $(CFLAGS)  version.c -o version.o

($< expands as the first pre-requisite - version.c - and $@ as the target - version.o).

Note: the solution you are using is very far from the normal make strategy. You should probably reconsider all this and base your Makefile on the classical target: pre-requisites dependency graph.

Upvotes: 1

Chris
Chris

Reputation: 335

The youltion was to add version.c at the very last to OBJECTFILES because it all depends of the order.

fileA.c
version.c
fileB.c

If touched version.c when it compiles fileA.c then version.c was recompile but not when version.c was touch when fileB.c was compiled because version.c was allready checked. So the solution is:

fileA.c
fileB.c
...
version.c

Upvotes: 0

Related Questions