Define a pre-processor variable for all the files in make

12,596

Add the compiler option -DFOOBAR to the make variable CFLAGS

Share:
12,596
mmtauqir
Author by

mmtauqir

Updated on July 18, 2022

Comments

  • mmtauqir
    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 using make.

    Is there a way to do this with some option to make or Makefile ?

    Thanks.

  • R.. GitHub STOP HELPING ICE
    R.. GitHub STOP HELPING ICE over 12 years
    It should be noted that this is not just a gcc-ism but part of the POSIX standard for C compiler invocation.
  • mmtauqir
    mmtauqir over 12 years
    Thanks...although I had a few folders each with its own Makefile and I had to add this CFLAGS=... 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
    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
    sherrellbc over 7 years
    @JensGustedt, Can the variable have a value?
  • sherrellbc
    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
    x-yuri about 6 years
    It took me a while to figure out, that I can't just do FOOBAR=1 make.