Define a pre-processor variable for all the files in make
12,596
Add the compiler option -DFOOBAR
to the make variable CFLAGS
Author by
mmtauqir
Updated on July 18, 2022Comments
-
mmtauqir almost 2 years
I have a number of
.c
and.h
files with some parts of the code disabled by putting the code block in pre-processor directives e.g.#ifdef FOOBAR // some code that can be compiled if needed by defining FOOBAR in pre-processor. #endif
No I need to define
FOOBAR
such that its defined for every file that is compiled by gcc usingmake
.Is there a way to do this with some option to make or
Makefile
?Thanks.
-
R.. GitHub STOP HELPING ICE over 12 yearsIt should be noted that this is not just a gcc-ism but part of the POSIX standard for C compiler invocation.
-
mmtauqir over 12 yearsThanks...although I had a few folders each with its own
Makefile
and I had to add thisCFLAGS=...
to each one of them, it worked. I wished there was such a thing while calling make. Like e.g.make -flags -DFOOBAR
or something similar. But thanks for then answer. -
Jens Gustedt over 12 years@mtahmed, there is. Just put the variable definitions on the command line of make,
make CFLAGS='-O3 -march=native -DFOOBAR'
-
sherrellbc over 7 years@JensGustedt, Can the variable have a value?
-
sherrellbc over 7 years@JensGustedt, Found it (for gcc).
-D'macro=definition'
. It does not seem to properly work without the outer single quotations. See here. -
x-yuri about 6 yearsIt took me a while to figure out, that I can't just do
FOOBAR=1 make
.