2016-03-01 74 views
1

我编译我的代码时给出了标志-std=c++11,我得到各种错误描述我应该使用相同的标志。另外,auto不被识别为一种类型。G ++似乎并不认可-std = C++ 11

的Makefile:

GCCPATH = /path/gcc/5.3.0 
CC = $(GCCPATH)/bin/g++ 
DARGS = -ggdb    #debug arguments 
CARGS = -std=c++11   #C arguments 
WARGS = -Wall -Wextra  #warning arguments 
AARGS = $(DARGS) $(CARGS) $(WARGS) #all arguments 
GCCLIBPATH = $(GCCPATH)/lib64 
LIBS = -l curl 
LIBD = -L $(GCCLIBPATH) -Wl,-rpath=$(GCCLIBPATH) 

.PHONY: webspider 

webspider: ../title/htmlstreamparser.o filesystem.o 
    $(CC) $(AARGS) -o [email protected] [email protected] $+ $(LIBS) $(LIBD) 

filesystem: 
    $(CC) $(AARGS) -c [email protected] 

的警告和错误,我得到:

warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 
warning: range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11 
error: ‘weblink’ does not name a type 
    for(auto weblink: weblinks) 

现在我的问题是:我应该怎么做才能让G ++认识到这一点清楚地定标志?
我也试图用-std=c++0x替换它,没有用。

编辑:
make全输出:

g++ -c -o filesystem.o filesystem.cpp 
In file included from filesystem.cpp:1:0: 
filesystem.hpp:23:36: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 
    std::string dir = getCurrentPath(); 
            ^
filesystem.cpp: In member function ‘std::__cxx11::string Filesystem::createMD5(std::__cxx11::string)’: 
filesystem.cpp:49:19: warning: range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11 
    for(long long c: result) 
       ^
filesystem.cpp: In member function ‘void Filesystem::createLinkIndex(std::__cxx11::string, strVec)’: 
filesystem.cpp:57:11: error: ‘weblink’ does not name a type 
    for(auto weblink: weblinks) { 
     ^
filesystem.cpp:61:1: error: expected ‘;’ before ‘}’ token 
} 
^ 
filesystem.cpp:61:1: error: expected primary-expression before ‘}’ token 
filesystem.cpp:61:1: error: expected ‘;’ before ‘}’ token 
filesystem.cpp:61:1: error: expected primary-expression before ‘}’ token 
filesystem.cpp:61:1: error: expected ‘)’ before ‘}’ token 
filesystem.cpp:61:1: error: expected primary-expression before ‘}’ token 
make: *** [filesystem.o] Error 1 
+6

你不应该有'CXXFLAGS = -std = C++ 11'吗? – NathanOliver

+0

您是否看到该标志是否传递给gcc并且只是无法识别,或者它实际上是否与makefile有关? – Anedar

+0

@NathanOliver这就是我想说的...... – callyalater

回答

5

问题是你不指定所有的依赖关系,特别是如何建立你所有的中间对象文件

所以会发生什么是make组成自己的规则,并无形中潜入他们,而你没有看。

控制这些implicit rules的方式是通过设置正确的predefined variables

CXX := $(GCCPATH)/bin/g++  # c++ compiler 
CPPFLAGS := -I/path/to/headers # preprocessor flags 
CXXFLAGS := -std=c++11   # compiler flags 
LDFLAGS := -L/path/to/libs  # linker flags 
LDLIBS := -lcurl    # libraries to link 
# etc... 

通过使用正确的预定义变量,而不是让你自己的,你可以建立一个时节省了大量的工作Makefile

+1

接受并不是因为它最能帮助我的答案,而是因为答案最能帮助人们。 –

0

最后,根据该意见,它是固定的,通过改变

filesystem: 
    $(CC) $(AARGS) -c [email protected] 

filesystem.o: filesystem.cpp 
    $(CC) $(AARGS) -c $+ 

的Makefile文件不明白,我试图让filesystem.o与规则filesystem: ...。当明确说明这一点时,它按预期工作。

这个方法优于Galik的答案是使用自己的变量的能力,虽然在这种情况下,由于它是一个小项目,所以没有那么多优势。