使用CXXFLAGS在我的makefile中支持C ++ 11支持

时间:2018-05-27 19:16:34

标签: c++ c++11 makefile compilation

我试图在我的学校服务器上编译我的程序,但是为了让我的程序能够编译,需要使用C ++ 11。我不确定我是否如何实现从终端编译时使用的g ++ -std = c ++ 11 * .cpp * .h -o programName。

这是我的makefile:

all, Project4, Project4.exe: Character.o Barbarian.o BlueMen.o Vampire.o 
Medusa.o HarryPotter.o LinkedList.o Queue.o Stack.o main.o
g++ main.o Character.o Barbarian.o BlueMen.o Vampire.o Medusa.o 
HarryPotter.o LinkedList.o Queue.o Stack.o -o Projec4

Character.o: Character.cpp Character.h
    g++ -c Character.cpp

Barbarian.o: Barbarian.cpp Barbarian.h
    g++ -c Barbarian.cpp

BlueMen.o: BlueMen.cpp BlueMen.h
    g++ -c BlueMen.cpp

Vampire.o: Vampire.cpp Vampire.h
    g++ -c Vampire.cpp

Medusa.o: Medusa.cpp Medusa.h
    g++ -c Medusa.cpp

HarryPotter.o: HarryPotter.cpp HarryPotter.h
    g++ -c HarryPotter.cpp

LinkedList.o: LinkedList.cpp LinkedList.h
    g++ -c LinkedList.cpp

Queue.o: Queue.cpp Queue.h
    g++ -c Queue.cpp

Stack.o: Stack.cpp Stack.h
    g++ -c Stack.cpp

main.o: main.cpp Character.h Barbarian.h BlueMen.h Vampire.h Medusa.h 
HarryPotter.h LinkedList.h Queue.h Stack.h
    g++ -c main.cpp

clean:
    -rm *.o

1 个答案:

答案 0 :(得分:0)

只需将必要的编译参数添加到makefile规则中:

all, Project4, Project4.exe: Character.o Barbarian.o BlueMen.o Vampire.o 
Medusa.o HarryPotter.o LinkedList.o Queue.o Stack.o main.o
g++ main.o Character.o Barbarian.o BlueMen.o Vampire.o Medusa.o 
HarryPotter.o LinkedList.o Queue.o Stack.o -std=c++11 -o Projec4

Character.o: Character.cpp Character.h
    g++ -c -std=c++11 Character.cpp

Barbarian.o: Barbarian.cpp Barbarian.h
    g++ -c -std=c++11 Barbarian.cpp

BlueMen.o: BlueMen.cpp BlueMen.h
    g++ -c -std=c++11 BlueMen.cpp

Vampire.o: Vampire.cpp Vampire.h
    g++ -c -std=c++11 Vampire.cpp

Medusa.o: Medusa.cpp Medusa.h
    g++ -c -std=c++11 Medusa.cpp

HarryPotter.o: HarryPotter.cpp HarryPotter.h
    g++ -c -std=c++11 HarryPotter.cpp

LinkedList.o: LinkedList.cpp LinkedList.h
    g++ -c -std=c++11 LinkedList.cpp

Queue.o: Queue.cpp Queue.h
    g++ -c -std=c++11 Queue.cpp

Stack.o: Stack.cpp Stack.h
    g++ -c -std=c++11 Stack.cpp

main.o: main.cpp Character.h Barbarian.h BlueMen.h Vampire.h Medusa.h 
HarryPotter.h LinkedList.h Queue.h Stack.h
    g++ -c -std=c++11 main.cpp

clean:
    -rm *.o

为了可维护性,您还可以添加一个包含所有必需的编译器标志的makefile变量:

CXXFLAGS := -std=c++11

all, Project4, Project4.exe: Character.o Barbarian.o BlueMen.o Vampire.o 
Medusa.o HarryPotter.o LinkedList.o Queue.o Stack.o main.o
g++ main.o Character.o Barbarian.o BlueMen.o Vampire.o Medusa.o 
HarryPotter.o LinkedList.o Queue.o Stack.o $(CXXFLAGS) -o Projec4

Character.o: Character.cpp Character.h
    g++ -c $(CXXFLAGS) Character.cpp

Barbarian.o: Barbarian.cpp Barbarian.h
    g++ -c $(CXXFLAGS) Barbarian.cpp

BlueMen.o: BlueMen.cpp BlueMen.h
    g++ -c $(CXXFLAGS) BlueMen.cpp

Vampire.o: Vampire.cpp Vampire.h
    g++ -c $(CXXFLAGS) Vampire.cpp

Medusa.o: Medusa.cpp Medusa.h
    g++ -c $(CXXFLAGS) Medusa.cpp

HarryPotter.o: HarryPotter.cpp HarryPotter.h
    g++ -c $(CXXFLAGS) HarryPotter.cpp

LinkedList.o: LinkedList.cpp LinkedList.h
    g++ -c $(CXXFLAGS) LinkedList.cpp

Queue.o: Queue.cpp Queue.h
    g++ -c $(CXXFLAGS) Queue.cpp

Stack.o: Stack.cpp Stack.h
    g++ -c $(CXXFLAGS) Stack.cpp

main.o: main.cpp Character.h Barbarian.h BlueMen.h Vampire.h Medusa.h 
HarryPotter.h LinkedList.h Queue.h Stack.h
    g++ -c $(CXXFLAGS) main.cpp

clean:
    -rm *.o

有关自定义makefile的更多信息,请访问:CFLAGS, CCFLAGS, CXXFLAGS - what exactly do these variables control?