4

Makefile

ifeq ($(wifiSim),1)
WIFISIM :=1
endif

all: test.cpp

test.cpp : test.o
        ./a.out

test.o :
        c++ test.cpp

test.cpp

#include <iostream>

using namespace std;

int main()
{    
        #ifdef WIFISIM
                cout << "Inside wifisim = 1" << endl;
        #else
              cout << "Outside wifisim = 1" << endl;
        #endif

        return 0;
}

I want to use the WIFISIM in the test.cpp. I am running make wifiSim=1 all But the else is being executed in test.cpp

Is there any way I can do it without doing any changes in the way the compilation for test.cpp is done, because I need to use this flag WIFISIM in many files and I do not want to change the way compilation for them is being done.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
a2n3k7it
  • 65
  • 1
  • 6

2 Answers2

10

You may do something like this

ifeq ($(wifiSim),1)
    WIFISIM := -DWIFISIM
endif

all: test.cpp

test.cpp : test.o
        ./a.out

test.o :
        c++ $(WIFISIM) test.cpp

"Is there any way I can do it without doing any changes in the way the compilation for "test.cpp" is done, because I need to use this flag WIFISIM in many files and I do not want to change the way compilation for them is being done."

No, there's no way without changing the compiler call action in the rule.

You should change your strategy writing the makefile. make actually supports implicit rules how to create a .o file from a .cpp and uses an action that looks like

$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c

Thus you could add the -DWIFISIM conditionally to the $(CPPFLAGS) or $(CXXFLAGS) variables, and it will be applied for all .cpp files compiled.

Sample using implicit rules:

ifeq ($(wifiSim),1)
    CXXFLAGS += -DWIFISIM
endif

SRC_FILES := test.cpp abc.cpp yxz.cpp
OBJ_FILES := $(patsubst %.cpp,%.o,$(SRC_FILES))

all: test

test: $(OBJ_FILES)
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Is there any way I can do it without doing any changes in the way the compilation for "test.cpp" is done, because I need to use this flag WIFISIM in many files and I do not want to change the way compilation for them is being done. – a2n3k7it May 17 '15 at 20:05
  • @a2n3k7it You should use the implicit compilation rules (`.o : .cpp`) then and add that to the `$(CXXFLAGS)` variable. – πάντα ῥεῖ May 17 '15 at 20:07
  • 1
    @a2n3k7it: No; not if your makefiles are written as shown. If you used macros everywhere, and/or the built-in C++ compilation rules, you might be in with a fighting chance, but not with the compilation lines written verbatim as shown in the question. – Jonathan Leffler May 17 '15 at 20:08
1

If you use GCC, you may use option -DWIFISIM as options passed to GCC/G++. Other compilers have similiar options, such as /D in Microsoft Visual Studio:

CXXFLAGS = 

ifeq ($(wifiSim),1)
CXXFLAGS += -DWIFISIM
endif

all: test.cpp

test.cpp : test.o
    ./a.out

test.o :
    c++ $(CXXFLAGS) test.cpp

Result:

$ make -n wifiSim=1
c++  -DWIFISIM test.cpp
./a.out
$ make -n
c++  test.cpp
./a.out
myaut
  • 11,174
  • 2
  • 30
  • 62