Allowing users to override CFLAGS, CXXFLAGS and friends

后端 未结 4 2189
面向向阳花
面向向阳花 2021-01-19 10:40

Typical makefiles often use the built-in variables CFLAGS, CXXFLAGS, CPPFLAGS and so on1 to set the flags passed to the C,

4条回答
  •  轮回少年
    2021-01-19 10:45

    The approach I prefer is to provide sensible default values to these common variables, but let users provide their own - overriding the default values.

    include $(wildcard makefile.in Makefile.in)
    
    BUILD ?= build
    CFLAGS ?= -O2 -fPIC -pedantic -Wall -Wextra -Wconversion
    

    This can be done by either environment variables, command line parameters like make CFLAGS=-g or persistently in a makefile.in.

    I am aware that this doesn't exactly pick up the issue you described in the questions, but I found use cases in which users want to compile a project with non-default flags should be able to

    1. Define these variables to their needs
    2. Check their defaults, preferably at the top of the makefile
    3. Maybe adjust the definitions in accordance to the defaults

    If someone wants to build with some special flags and is incapable of these steps, there will be some more serious problems anyhow.

    This approach will not scale well when the build becomes more involved and the defaults are set across a larger makefile and dependent on other conditions.

提交回复
热议问题