Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a flag from C++FLAGS in Makefile?

Tags:

c++

c

makefile

I've got a Makefile that includes another makefile that sets a lot of defaults. I can't edit the included makefile and I'd like to change the value of C++FLAGS in my makefile even though it is set in the included makefile. Specifically, I'd like to remove the optimization flag from C++FLAGS whenever debug=1.

I tried the following:

C++FLAGS=$(filter-out -O3,$(C++FLAGS))

Which fails with the following error:

Recursive variable `C++FLAGS' references itself (eventually).  Stop.

It seems like doing something like this should be possible, anybody know the secret?

like image 444
Paul Wicks Avatar asked Mar 08 '12 02:03

Paul Wicks


People also ask

What is C flag in Makefile?

CFLAGS and CXXFLAGS are either the name of environment variables or of Makefile variables that can be set to specify additional switches to be passed to a compiler in the process of building computer software.

How do I add Cflags in Makefile?

It should be CFLAGS := -Wall -Wextra $(CFLAGS) , the difference is that CFLAGS is explicitly appended. So for example, you may set -Og , but user don't want optimization and passes CFLAGS=-O0 on command line. By using CFLAGS += -Og your -Og will take over the user provided value.

How do I enable flags in Makefile?

The only way of doing that is to edit the makefile to change the options. There is no convenient way to override the options without modifying the makefile . This is where make flags come into play. Flags in make are just variables containing options that should be passed to the tools used in the compilation process.


1 Answers

C++FLAGS:=$(filter-out -O3,$(C++FLAGS))

The := assignment immediately evaluates the rvalue and this should therefore work. = on the other hand has delayed expansion semantics (i.e. the C++FLAGS will expand whenever the lvalue gets used, which leads to recursion).

like image 153
0xC0000022L Avatar answered Sep 22 '22 04:09

0xC0000022L