How to pass macro definition from “make” command line arguments (-D) to C source code?

前端 未结 6 1909
醉话见心
醉话见心 2020-12-04 07:40

I usually pass macro definitions from \"make command line\" to a \"makefile\" using the option : -Dname=value. The definition is accessible inside the makefile.

I al

6条回答
  •  北海茫月
    2020-12-04 08:17

    Call make command this way:

    make CFLAGS=-Dvar=42
    

    And be sure to use $(CFLAGS) in your compile command in the Makefile. As @jørgensen mentioned , putting the variable assignment after the make command will override the CFLAGS value already defined the Makefile.

    Alternatively you could set -Dvar=42 in another variable than CFLAGS and then reuse this variable in CFLAGS to avoid completely overriding CFLAGS.

提交回复
热议问题