GCC使用开方,而不包括math.h中 [英] Gcc uses sqrt without including math.h

查看:363
本文介绍了GCC使用开方,而不包括math.h中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人知道为什么这个C程序编译和使用math.h中的开方?

Anyone knows why this c program compiles and uses the sqrt of math.h?

这将输出2.236068

this would output 2.236068

的main.c

#include <stdio.h>
#include "math_utils.h"

int main(void){
  printf("%f\n", sqrt(5));
  return 0;
}

math_utils.h

math_utils.h

#ifndef MATH_UTILS_Hs
#define MATH_UTILS_Hs

double sqrt(double number){
  return number + 5;
}

#endif // MATH_UTILS_Hs

我目前在Windows上使用MinGW的GCC

I am currently using mingw GCC on windows

推荐答案

GCC执行它期望标准库函数的行为类似于标准说,把电话放到C标准库到更高效的机器code优化。例如,很可能是海湾合作委员会发出你的开方一个 FSQRT 指令()电话,从来不叫您的自定义的sqrt()的。

gcc performs an optimization where it expects standard library functions to behave like the standard says to turn calls into the C standard library into more efficient machine code. For example, it's likely that gcc emits a single fsqrt instruction for your sqrt() call, never calling your custom sqrt() at all.

您可以通过提供 -fno-内置关闭此行为把这种优化关闭所有公认的功能或通过提供 -fno-内置 - 功能关闭此优化功能而已。例如, -fno-内置-开方将使GCC孝敬非标的sqrt()

You can turn off this behaviour by supplying -fno-builtin to turn this optimization off for all recognized functions or by supplying -fno-builtin-function to turn off this optimization for function only. For example, -fno-builtin-sqrt would make gcc honour your non-standard sqrt().

这篇关于GCC使用开方,而不包括math.h中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆