简体   繁体   English

G ++似乎无法识别-std = c ++ 11

[英]G++ does not seem to recognize -std=c++11

I compile my code with the flag -std=c++11 given, and I get all kinds of errors depicting that I should use the same flag. 我使用给定的-std=c++11标志编译代码,并且遇到各种错误,这些错误描述了我应该使用相同的标志。 Also, auto is not recognised being a type. 另外,无法将auto识别为一种类型。

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 $@ $@.cpp $+ $(LIBS) $(LIBD)

filesystem:
    $(CC) $(AARGS) -c $@.cpp

The warnings and errors I get: 我得到的警告和错误:

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)

Now my question is: What should I do to make g++ recognise this clearly given flag? 现在我的问题是:我应该怎么做才能使g ++识别清楚地给出的标志?
I also tried to replace it with -std=c++0x , to no avail. 我也尝试用-std=c++0x替换它,但无济于事。

EDIT: 编辑:
Full output of make : 完整输出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

The problem is you do not specify all your dependencies, in particular how to build all your intermediate object files . 问题是您没有指定所有依赖项,特别是如何构建所有中间目标文件

So what happens is make makes up its own rules and invisibly sneaks them in while you're not looking. 因此,发生的事情就是make自己的规则,并且在您不看的时候就潜入了它们。

The way to control these implicit rules is through setting the correct 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...

By using the correct predefined variables , rather than making up your own, you can save a lot of work when building a Makefile . 通过使用正确的预定义变量 ,而不是自己编写变量 ,可以在构建Makefile时节省大量工作。

In the end, based on the comments, it was fixed by changing 最后,根据评论,通过更改

filesystem:
    $(CC) $(AARGS) -c $@.cpp

to

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

as Makefile did not understand that I was trying to make filesystem.o with the rule filesystem: ... . 因为Makefile无法理解我正在尝试使用规则filesystem: ...来制作filesystem.o When stating this explicitely, it worked as intended. 明确说明时,它按预期工作。

Advantage of this method over the answer of Galik is the ability to use own variables, albeit in this case not that much of an advantage since it is a small project. 与Galik相比,此方法的优势在于可以使用自己的变量,尽管在这种情况下,由于它是一个小项目,所以优势不大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM