【问题标题】:g++ compile options with -std=c++14 compile error showing Werror=c++11-compatg++ 编译选项与 -std=c++14 编译错误显示 Werror=c++11-compat
【发布时间】:2018-05-12 22:24:45
【问题描述】:

我的环境 Arch Linux,gcc 7.2

我正在学习 C++,我正在使用关键字 constexpr 来定义一个常量,编译时它给了我一个错误消息
错误:identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]

我可以使用默认 g++ 编译我的程序,但无法使用 -std=c++14 和 -Werror 进行编译

我使用的命令是:

g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto

我相信-Werror 选项导致了这个问题。但问题是什么?谁能告诉我?

#include <iostream>

int main() {
    constexpr double yen_dollar = 0.107;
    std::cout << yen_dollar << std::endl;
    return 0;
}
test.cpp:4:5: error: identifier ‘constexpr’ is a keyword in C++11 [-Werror=c++11-compat]
     constexpr double yen_dollar = 0.107;
     ^~~~~~~~~
test.cpp: In function ‘int main()’:
test.cpp:4:5: error: ‘constexpr’ was not declared in this scope
test.cpp:5:16: error: ‘yen_dollar’ was not declared in this scope
     std::cout << yen_dollar << std::endl;

【问题讨论】:

  • 很奇怪。 This online version of GCC 7.2 正在顺利编译它。你能提供更多细节吗?喜欢确切的命令行参数吗?
  • 你能显示你用来构建源代码的 exact 命令行吗?将其复制(作为文本)并将其粘贴到问题的正文中,无需任何修改。
  • g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto 谢谢
  • 去掉-ansi标志。

标签: c++ c++11 gcc c++14


【解决方案1】:

从 GCC 文档§3.4 Options Controlling C Dialect,可以阅读:

-ansi

  In C mode, this is equivalent to -std=c90. In C++ mode, it is equivalent to -std=c++98. 

既然,你编译了

g++ -std=c++14 -O2 -Wall -Werror -Wextra -ansi -flto

-ansi-std=c++98 覆盖-std=c++14。这就是无法识别constexpr 的原因。

解决方案:去掉-ansi 标志。

【讨论】:

    猜你喜欢
    • 2012-05-08
    • 2019-11-07
    • 2015-05-03
    • 1970-01-01
    • 2015-11-05
    • 2015-09-12
    • 2017-04-09
    • 1970-01-01
    相关资源
    最近更新 更多