Gcc uses sqrt without including math.h

Does anyone know why this c-program compiles and uses sqrt from math.h?

this will exit 2.236068

main.c

#include <stdio.h> #include "math_utils.h" int main(void){ printf("%f\n", sqrt(5)); return 0; } 

math_utils.h

 #ifndef MATH_UTILS_Hs #define MATH_UTILS_Hs double sqrt(double number){ return number + 5; } #endif // MATH_UTILS_Hs 

I am currently using mingw gcc for windows

+5
source share
1 answer

gcc performs an optimization where it expects standard library functions to behave as specified in the standard in order to turn calls into the standard C library into more efficient machine code. For example, it is likely that gcc emits one fsqrt command for your sqrt() call, never calling your custom sqrt() at all.

You can disable this behavior by providing -fno-builtin to disable this optimization for all recognized functions, or by supplying -fno-builtin-function to disable this optimization only for function . For example, -fno-builtin-sqrt will honor gcc with your non-standard sqrt() .

+9
source

Source: https://habr.com/ru/post/1232354/


All Articles