cgcc

I used pow() function in my C program but did not link to -lm it still worked. Why?


I was reading a book on GCC. It said only the standard library is linked by default for any C program. Since the pow() is not in the standard library, I will have to link to it using the -lm flag. However, when I compiled, I simply used:

gcc hello.c -o hello

and it still worked..

And there is another similar problem, the book also said that if you have printf("%f\n", 4); in your C program and if you compile WITHOUT -Wall option, no warning will be issued. However, I tried compiling it without the -Wall option but I still got a warning:

hello.c:6:2: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat]

Why is this? The book said I have to supply -lm and -Wall in order to make my program compiled and get the warning but I did not use either of them but I still got my program compiled and got the warning?


Solution

  • GCC supplies several standard library functions as built-ins:

    GCC provides a large number of built-in functions other than the ones mentioned above. Some of these are for internal use in the processing of exceptions or variable-length argument lists and will not be documented here because they may change from time to time; we do not recommend general use of these functions.

    The remaining functions are provided for optimization purposes.

    If you look at the list of built-ins, you'll see that pow is one of them.

    If you add -fno-builtin to your compiler options, you should get the linker error that you're expecting.