【问题标题】:How does GNU make understand if a flag is set?如果设置了标志,GNU 如何理解?
【发布时间】:2014-03-31 07:53:51
【问题描述】:

我正在开发一个带有 makefile 的 C++ 项目。我必须对其进行一些修改,但在此之前,我有一个关于标志在 GNU make 中的解释方式的问题。详细来说,在下面的 sn-p 中,我有两个选项可以在我的项目编译期间启用或禁用某个功能,

    # 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 时,它被禁用了。

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

非常感谢您的任何解释。

【问题讨论】:

    标签: c++ makefile compiler-flags


    【解决方案1】:

    因为你写了

    CXXFLAGS += $(CXXFLAGS_$(FEATURE))
    

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

    CXXFLAGS += $(CXXFLAGS_off)
    

    其中,因为你也定义了

    CXXFLAGS_off = -DFEATURE_DISABLED
    

    依次扩展为

    CXXFLAGS += -DFEATURE_DISABLED
    

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

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 1970-01-01
      • 2017-10-06
      • 2016-07-25
      • 1970-01-01
      • 2010-11-23
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多