GNU 如何理解是否设置了一个标志

How does GNU make understand if a flag is set?

本文关键字:一个 标志 何理解 是否 设置 GNU      更新时间:2023-10-16

我正在处理一个带有makefile的C++项目。我必须对它进行一些修改,但在此之前,我有一个关于 GNU 中解释标志的方式的问题。为了详细说明,在下面的代码片段中,我有两个选项可以在编译项目期间启用或禁用功能,

    # Two options for a feature:
      FEATURE=on off
    # other lines in the make file
    # By default, the feature is turned off
      ifndef FEATURE
       FEATURE = off
      endif
   # other lines in the make file
   # A number of other flags are defined here
   # I have defined a flag to indicate that my feature is disabled
     CXXFLAGS_off = -DFEATURE_DISABLED
   # Adding all the flags
     CXXFLAGS += $(CXXFLAGS_$(FEATURE)) #Other flags also added

现在,在我的代码中的某个地方,我有这样一行:

    #ifdef FEATURE_DISABLED
       //Don't invoke the functions for the feature
    #else
      //Invoke the functions for the feature
    #endif

现在在编译过程中,当我说使 FEATURE = on 时,我看到程序工作正常,启用了该功能。当我说使功能=关闭时,它被禁用。

然而,我的问题是我不完全理解编译器如何解释我的选择。例如,我只是说,"make FEATURE = off",这一行如何映射到启用 off 标志的事实,以及如何在关闭该功能的情况下编译代码?正如我上面所写的,我确实将我的功能的标志添加到 CXXFLAGS 中,但是如何理解 FEATURE = off 意味着设置了 FEATURE_DISABLED 标志?

非常感谢您的任何解释。

因为你写了

CXXFLAGS += $(CXXFLAGS_$(FEATURE))

当您在make命令行上提供FEATURE = off时,它将扩展到

CXXFLAGS += $(CXXFLAGS_off)

其中,因为您还定义了

CXXFLAGS_off = -DFEATURE_DISABLED

反过来扩展到

CXXFLAGS += -DFEATURE_DISABLED

这意味着编译器将使用-DFEATURE_DISABLED作为额外的参数运行。