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

    # 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

现在,在编译过程中,当我说make FEATURE = on时,我看到程序在启用该功能的情况下运行良好。当我说使功能=关时,它被禁用。

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

非常感谢您的解释。

最佳答案

因为你写了

CXXFLAGS += $(CXXFLAGS_$(FEATURE))

当您在FEATURE = off命令行上提供make时,它将扩展为
CXXFLAGS += $(CXXFLAGS_off)

因为您还定义了
CXXFLAGS_off = -DFEATURE_DISABLED

反过来扩展到
CXXFLAGS += -DFEATURE_DISABLED

这意味着编译器将使用-DFEATURE_DISABLED作为附加参数运行。

关于c++ - GNU如何理解是否设置了标志?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22756269/

10-13 01:21