如果设置了标志,GNU如何理解?

时间:2014-03-31 07:53:52

标签: c++ makefile compiler-flags

我正在使用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时,它被禁用。

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

非常感谢您的任何解释。

1 个答案:

答案 0 :(得分:4)

因为你写了

CXXFLAGS += $(CXXFLAGS_$(FEATURE))

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

CXXFLAGS += $(CXXFLAGS_off)

,因为你还定义了

CXXFLAGS_off = -DFEATURE_DISABLED

反过来扩展为

CXXFLAGS += -DFEATURE_DISABLED

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