[英]How does GNU make understand if a flag is set?
我正在使用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))
當在make
命令行上提供FEATURE = off
時,它將擴展為
CXXFLAGS += $(CXXFLAGS_off)
因為您還定義了
CXXFLAGS_off = -DFEATURE_DISABLED
反過來擴展到
CXXFLAGS += -DFEATURE_DISABLED
這意味着編譯器將使用-DFEATURE_DISABLED
作為附加參數運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.