2

I want to add -std=c++11 to my makefile but I do not Where to add, here is my code:

hw07: test.o functions.o
    g++ test.o functions.o -o hw07
test.o: test.cpp headerfile.h
    g++ -c test.cpp
functions.o: functions.cpp  headerfile.h
    g++ -c functions.cpp
clean:
    rm *.o hw07

in the above code where should I add the stdc++11 code, please help me out about...

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Ashraf Yawar
  • 857
  • 1
  • 7
  • 15

2 Answers2

5

Instead of spelling out all of the rules and all of the commands, use variables and implicit rules to build your program:

CXXFLAGS = -std=c++11

hw07: test.o functions.o

test.o: test.cpp headerfile.h

functions.o: functions.cpp  headerfile.h

clean:
    rm *.o hw07

This will have make build the object files using $(CXXFLAGS) as the options to pass to the compiler. Then make will build the program hw07 using the files listed in its dependencies.


Other flags that are good to have when compiling the source files are -Wall and -Wextra. Those enable more warning messages from the compiler, that in almost all cases point out suspect things that could lead to problems.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You can just add -std=c++11 after each g++:

hw07: test.o functions.o
    g++ -std=c+++11 test.o functions.o -o hw07
test.o: test.cpp headerfile.h
    g++ -std=c+++11 -c test.cpp
functions.o: functions.cpp  headerfile.h
    g++ -std=c+++11 -c functions.cpp
clean:
    rm *.o hw07

Also you can use a variable:

CPP_FLAGS='-std=c++11'

hw07: test.o functions.o
    g++ ${CPP_FLAGS} test.o functions.o -o hw07
test.o: test.cpp headerfile.h
    g++ ${CPP_FLAGS} -c test.cpp
functions.o: functions.cpp  headerfile.h
    g++ ${CPP_FLAGS} -c functions.cpp
clean:
    rm *.o hw07
sir__finley
  • 140
  • 3
  • 6