次の方法でうまくいくと思います。の実行時に環境変数を定義しますmake
。Makefile で、環境変数のステータスを確認します。ステータスに応じて、コードのコンパイル時に g++ に渡されるオプションを定義します。g++ 前処理段階でオプションを使用して、ファイルに何を含めるかを決定します (例: source.cpp)。
コマンド
make FEATURE=1
メイクファイル
ifeq ($(FEATURE), 1) #at this point, the makefile checks if FEATURE is enabled
OPTS = -DINCLUDE_FEATURE #variable passed to g++
endif
object:
g++ $(OPTS) source.cpp -o executable //OPTS may contain -DINCLUDE_FEATURE
ソース.cpp
#ifdef INCLUDE_FEATURE
#include feature.h
//functions that get compiled when feature is enabled
void FeatureFunction1() {
//blah
}
void FeatureFunction2() {
//blah
}
#endif
FEATURE が (任意の値として) 渡されたかどうかを確認するには:
ifdef FEATURE
#do something based on it
else
# feature is not defined. Maybe set it to default value
FEATURE=0
endif