0
votes

I would like to append this string to the CFLAGS without modifying the makefile itself:

-frandom-seed=$(shell md5sum $< | sed 's/\(.*\) .*/\1/')

Simply appending to the CFLAGS variable before running make doesn't work, since it can't be evaluated at that moment.

Is there a way do it without modifying the makefile?

1
How does the makefile define (and use) the CFLAGS variable? - Etan Reisner

1 Answers

0
votes

Option 1:

For this option, you will have to be able to modify your Makefile once. Change the line in your Makefile that modifies or sets CFLAGS to override CFLAGS+=<current flags already being set in CFLAGS>. So, if you currently have CFLAGS=-std=c99 in your Makefile, change that to override CFLAGS+=-std=c99

Then, say you wanted to add -g for compilation, simply call make like so:

make CFLAGS=-g

This will prepend -g to -std=c99.

See the override directive for more info.

Option 2:

This is possible using the -e option passed when running make.

-e gives variables taken from the environment precedence over variables from makefiles.

For example, take the following Makefile:

CFLAGS=-std=c++11
echoCFLAGS:
    @echo $(CFLAGS)

If you want to override CFLAGS with something else(-std=c99), then simply:

bash -c "export CFLAGS=-std=c++99"
make -e echoCFLAGS

This outputs "-std=c99"

Issues with Option 2

1) This will not allow you to append in the sense you are wanting, but you could manually append -frandom-seed=$(shell md5sum $< | sed 's/\(.*\) .*/\1/') onto whatever the original value of CFLAGS is in the Makefile to get what you want.

2) The -e option "is not recommended practice" as stated in the GNU make manual. This is due to the fact that "this would cause different users to get different results from the same makefile"(GNU make Manual).

See this and this link for more info.