[英]How does GNU make understand if a flag is set?
I am working on a C++ project with a makefile. 我正在使用Makefile进行C ++项目。 I have to make several modifications to it but before that, I have a question about the way flags are interpreted in GNU make.
我必须对其进行一些修改,但是在此之前,我有一个关于在GNU make中解释标志的方式的问题。 To elaborate, in the following snippet, I have two options to enable or disable a feature during the compilation of my project,
详细来说,在以下代码段中,我有两个选项可以在项目编译期间启用或禁用功能,
# 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
Now, somewhere in my code, I have this line: 现在,在我的代码中的某处,我有这行:
#ifdef FEATURE_DISABLED
//Don't invoke the functions for the feature
#else
//Invoke the functions for the feature
#endif
Now during compilation, when I say make FEATURE = on , I see that the program works fine, with the feature enabled. 现在,在编译过程中,当我说make FEATURE = on时,我看到该程序在启用该功能的情况下工作正常。 When I say make FEATURE = off, it is disabled.
当我说使功能=关时,它被禁用。
My problem however is that I don't exactly understand how the compiler is interpreting my choice. 但是,我的问题是我不完全了解编译器如何解释我的选择。 For example, I am just saying, "make FEATURE = off", how is this line mapped to the fact that the flag for off is enabled and how does the code get compiled with the feature turned off?
例如,我只是说“ make FEATURE = off”,这行代码如何映射到启用了off标志的事实,以及如何在功能关闭的情况下编译代码? As I wrote above, I do add the flags for my feature to the CXXFLAGS, but how does make understand that FEATURE = off means that the FEATURE_DISABLED flag is set?
正如我在上面写的,我确实将功能的标志添加到了CXXFLAGS,但是如何理解FEATURE = off意味着设置了FEATURE_DISABLED标志?
Thank you very much for any explanation. 非常感谢您的解释。
Because you wrote 因为你写了
CXXFLAGS += $(CXXFLAGS_$(FEATURE))
which, when you supply FEATURE = off
on the make
command line, will expand to 当在
make
命令行上提供FEATURE = off
时,它将扩展为
CXXFLAGS += $(CXXFLAGS_off)
which, because you have also defined 因为您还定义了
CXXFLAGS_off = -DFEATURE_DISABLED
in turn expands to 反过来扩展到
CXXFLAGS += -DFEATURE_DISABLED
which means that the compiler will be run with -DFEATURE_DISABLED
as an extra argument. 这意味着编译器将使用
-DFEATURE_DISABLED
作为附加参数运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.