【发布时间】:2020-08-17 21:11:47
【问题描述】:
编辑: Strerror 似乎有效。例如,如果 errno = ERANGE,则输出“Result too large”。
问题依旧是errno没有从0变。
在 Xcode 中,我一直在尝试使用下面的短代码 cerrno 和 strerror。 Xcode 返回
sqrt(-1) = 南
未定义错误:0
而不是
sqrt(-1) = -nan
域外的数值参数,
例如 cpp.sh。
为什么会这样?
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cerrno>
#include <cstring>
using namespace std;
int main() {
errno = 0;
cout << "sqrt(-1) = " << sqrt(-1) << endl;
cout << strerror(errno) << endl << endl;
return(0);
}
【问题讨论】:
-
主要是为了满足我的好奇心(但可能会有所帮助),你能告诉我们你的编译器对
math_errhandling宏的值吗?例如添加这一行:cout << "math_errhandling = " << math_errhandling << endl;? -
嗨,它添加了“math_errhandling = 2”。
-
That explains it 然后。
-
好吧——问题就在这里!为了设置
errno,该宏必须包含1。 (它应该是 3 以符合 C++11,IIRC)。但是如何解决它......不知道。 -
您能否查看您的详细设置和/或编译器命令行选项以查看是否在任何地方设置了
-fno-math-errno选项;或查看您的标头以获取 __NO_MATH_ERRNO__的定义。或者,尝试在#include...行之前添加#undef __NO_MATH_ERRNO__。
标签: c++ xcode compiler-errors c-strings