views:

51

answers:

2

I'm using a Makefile to build an embedded project. I've inherited the project from numerous previous developers who haven't been using Make to its full potential, and I'd like to be able to specify the project version in the makefile using defines on the build command. However, there's already a build rule that builds all the object (.o) files. Is there any way to override that build rule for a specific object file so that I can add -D flags to the compiler ?

Another reason I'd like to be able to specify the project version in the makefile is so that I can have it generate artifacts with the build version in the names of the resulting files produced by the build process.

A: 
Beta
A: 

If you are using GNU make and you only want to change compiler options, you can use target-specific variables, like so:

x.o: CFLAGS += -DEXTRA_SYMBOL_FOR_X

This also works recursively, i.e. the target-specific value for x.o also is in effect for all targets which x.o depends on, meaning that if you build multiple executables in your makefile, you can set a target-specific variable on the executable itself, which will be in effect for all the object files:

foo: CFLAGS += -DEXTRA_SYMBOL_FOR_FOO_APP
JesperE