【问题标题】:G++ does not seem to recognize -std=c++11G++ 似乎无法识别 -std=c++11
【发布时间】:2016-03-22 07:02:36
【问题描述】:

我用给定的标志-std=c++11 编译我的代码,我得到了各种错误,表明我应该使用相同的标志。此外,auto 未被识别为一种类型。

生成文件:

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

我得到的警告和错误:

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

【问题讨论】:

  • 你不应该有CXXFLAGS=-std=c++11吗?
  • 你能make VERBOSE=1 验证传递给 gcc 的选项是否正确吗?
  • @NathanOliver Makefile 中变量的名称是否重要?
  • 如果您的构建依赖于 implicit 命令,那么是的,名称 do 很重要。尝试设置CXXFLAGS
  • 这里有一些非常可疑的东西——要么不是正确的 makefile,要么你有多个(在不同的目录或不同的名称)。

标签: c++ c++11 makefile


【解决方案1】:

问题是您没有指定所有依赖项,特别是如何构建所有中间目标文件

所以发生的事情是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 时节省大量工作。

【讨论】:

  • 接受不是因为它是对我帮助最大的答案,而是因为它是对一般人最有帮助的答案。
【解决方案2】:

最后在cmets的基础上,通过修改修复了

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

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

因为 Makefile 不明白我试图使用规则 filesystem: ... 来制作 filesystem.o。当明确说明这一点时,它按预期工作。

这种方法相对于 Galik 的答案的优势是能够使用自己的变量,尽管在这种情况下并没有太大的优势,因为它是一个小项目。

【讨论】:

    猜你喜欢
    • 2015-08-16
    • 2023-03-17
    • 2015-10-14
    • 2013-01-18
    • 1970-01-01
    • 2014-07-06
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多