Do: override flag

Am I a little confused with the answers to the Quick way to override the -Werror flag?

So I ask my specific question here.

I have several Makefiles working together, and CFLAGS was installed along the way (-Werror -Wall .. and many others)

But in one of the Make files, I want the errors not to be treated as warnings and therefore I would like to remove the -Werror flag.

What would be the best way to achieve this, so only for this Makefile, -Werror flag is removed, and for the rest normal execution is performed?

Thank you Sunny

+2
source share
2 answers

Simplest way

It looks like you can call

gcc -c ... -Werror ... -Wno-error ... 

without GCC filing (GCC 4.7.1). That way, you can add -Wno-error to CFLAGS created elsewhere in the makefile where you need it. If you use GNU make , in one makefile you can add:

 CFLAGS += -Wno-error 

possible only for the only purpose that he needs.

Harder way

Otherwise, you will need a system to create CFLAGS from components. What I have in the makefile that I use to test answers to questions on SO:

 WFLAG1 = -Wall WFLAG2 = -Wextra WFLAG3 = -Wmissing-prototypes WFLAG4 = -Wstrict-prototypes WFLAG5 = -Wold-style-definition WFLAG6 = WFLAGS = ${WFLAG1} ${WFLAG2} ${WFLAG3} ${WFLAG4} ${WFLAG5} ${WFLAG6} SFLAGS = -std=c99 GFLAGS = -g OFLAGS = -O3 UFLAGS = IFLAG1 = -I${HOME}/inc IFLAGS = # ${IFLAG1} CFLAGS = ${OFLAGS} ${GFLAGS} ${IFLAGS} ${SFLAGS} ${WFLAGS} ${UFLAGS} 

The main thing is that each flag is independently configured; I can control the warning flags by setting any of ${WFLAG1} to ${WFLAG6} , or by setting the ${WFLAGS} option on the command line or (really) by setting ${CFLAGS} . But since each of them is individually configurable and can easily configure alerts (the main problem is determined by which WFLAGn needs to be knocked down).

UFLAGS are "user flags" and are set only on the command line; I can add more flags to my command line by setting it.

This method is more complicated because it requires changing the central part of your makefile system where you install CFLAGS. It is also less likely that your peers are clear at a glance.

+2
source

The correct way to do this is with the filter-out function.

Placed

 CFLAGS := $(filter-out -Werror,$(CFLAGS)) 

in the Makefile where you want to override this, and the -Werror CFLAGS part will be deleted in this Makefile.

You can even use this to override flags for a single target using variable values ​​for the target :

 CFLAGS = -Werror all: foo bar foo: echo cc $(CFLAGS) -o $@ bar: CFLAGS := $(filter-out -Werror,$(CFLAGS)) bar: echo cc $(CFLAGS) -o $@ 

foo will be built by default CFLAGS containing -Werror , but bar will be built without.

This is a universal solution that works for all arguments for all programs, and does not require each program to provide --no-foo for each --foo parameter. Since it cannot be executed from the Make command line, it does not directly answer the question with which you are connected. But redefining Make variables from the command line to make the material create is a pretty good way to make your loose code even less maintenance friendly!

+4
source

All Articles