【发布时间】:2015-02-15 08:24:54
【问题描述】:
我正在尝试使用带有以下 makefile 的 g++ 4.8.2 来使用一些 C++11 功能
CC=g++
DEBUG=-g
CFLAGS=-c -Wall -std=c++11 $(DEBUG)
LFLAGS = -Wall -std=c++11 $(DEBUG)
SOURCES=test.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=test
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LFLAGS) $(OBJECTS) -o $@ -std=c++11
.cpp .o:
$(CC) $(CFLAGS) $< -o $@ -std=c++11
clean:
rm -rf *o $(EXECUTABLE)
但是当我调用“make”时,这是我得到的错误消息
$ make
g++ -c -o test.o test.cpp
test.cpp: In function ‘int main()’:
test.cpp:18:15: error: range-based ‘for’ loops are not allowed in C++98 mode
for (int i : {2, 3, 5, 7, 9, 13, 17, 19})
^
make: *** [test.o] Error 1
在我看来,-std=c++11 没有被选中,所以我试图在很多不同的地方抛出该选项,但仍然出现同样的错误。
目前的解决方法是直接使用命令行,这对我有用
$ cat test.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" << endl;
for (int i : {2, 3, 5, 7, 9, 13, 17, 19})
{
cout << i << " ";
}
cout << endl;
return 0;
}
$ g++ -std=c++11 test.cpp -o test -W
$ ./test
Hello World
2 3 5 7 9 13 17 19
我只是想知道为什么 makefile 不做同样的事情,以及如何更新 makefile 以使用 -std=c++11 选项。
【问题讨论】:
-
-std被设置了两次,分别在 CFLAGS 和规则中。