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

Ash*_*war 2 makefile c++11

我想将 -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
Run Code Online (Sandbox Code Playgroud)

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

Som*_*ude 5

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

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
Run Code Online (Sandbox Code Playgroud)

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


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