繁体   English   中英

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

[英]g++ compile options with -std=c++14 compile error showing Werror=c++11-compat

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

从GCC文档第3.4节“控制C语言的选项”中 ,可以看到:

 -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标志。

暂无
暂无

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

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