g ++编译选项-std = c ++ 14编译错误显示Werror = c ++ 11-compat

时间:2017-11-29 09:10:19

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

我的环境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;

1 个答案:

答案 0 :(得分:5)

从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++14覆盖-std=c++98。这就是为什么constexpr无法识别的原因。

解决方案:摆脱-ansi标志。

相关问题