Gcc uses sqrt without including math.h

前端 未结 1 1856
有刺的猬
有刺的猬 2020-12-20 19:27

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

this would output 2.236068

main.c

#include 
#include \"m         


        
相关标签:
1条回答
  • 2020-12-20 20:02

    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.

    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().

    0 讨论(0)
提交回复
热议问题