如何添加-std = c ++ 11来制作文件

时间:2019-05-22 10:20:09

标签: c++11 makefile

我想将-std = c ++ 11添加到我的makefile中,但是我不希望在何处添加,这是我的代码:

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

在上面的代码中我应该在哪里添加stdc ++ 11代码,请帮助我...

2 个答案:

答案 0 :(得分:2)

使用变量隐式规则来构建程序,而不是拼出所有规则和所有命令:

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

这将make使用$(CXXFLAGS)作为传递给编译器的选项来构建目标文件。然后make将使用依赖项中列出的文件来构建程序hw07


编译源文件时最好使用的其他标志是-Wall-Wextra。这些启用了来自编译器的更多警告消息,几乎在所有情况下都指出了可能导致问题的可疑事物。

答案 1 :(得分:0)

您只需在每个-std=c++11之后添加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

还可以使用变量:

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