问题
when I try to compile this simple code from terminal:
#include<stdio.h>
int main(void)
{
printf("%f\n",sqrt(10));
return 0;
}
using
gcc main.c
command, it gets compiled and a.out gives correct answer. It means that that maths functions are added in C standard library which gets linked automatically.
But if compile the same code in Eclipse IDE without adding any library to properties, it gives undefined reference error. It means maths functions are not part of C standard library.
What is the truth?
回答1:
You may be seeing constant folding here, where using a constant in the math function call will cause the compiler to calculate the function and omit the call to the math library all together.
If we check out the docs for Other Built-in Functions Provided by GCC says(emphasis mine):
GCC includes built-in versions of many of the functions in the standard C library. The versions prefixed with _builtin are always treated as having the same meaning as the C library function even if you specify the -fno-builtin option. (see C Dialect Options) Many of these functions are only optimized in certain cases; if they are not optimized in a particular case, a call to the library function is emitted.
If we look at this slightly modified live example which uses the following code:
#include <stdio.h>
#include <math.h>
int main(void)
{
printf("%d\n",(int)sqrt(25));
return 0;
}
we see the following assembly generated by gcc
:
movl $5, %esi
movq %rax, %rdi
movl $0, %eax
call printf
so we see 5
is moved into esi
which in the x64 abi is the second argument to to the calling function and is the result of sqrt(25)
there is no call to sqrt
at all.
Note you are missing:
#include <math.h>
Update
Built-ins are a gcc extension the built-in link above explains which ones are used in which mode and they should all have the same meaning as the standard functions.
If you are concerned about your code being standards compliant then you can check out the Options Controlling C Dialect section of the manual. You can use -std
to specify which standard you want to comply with and -pedantic
to enable warnings when you are using a feature that does not conform with the standard or -pedantic-errors
to make the warnings an error. So for example using
gcc -std=c99 -pedantic
would generate a warning when you used a feature that was compliant with the C99 standard for example zero length arrays.
We can also use -fno-builtin
to disable some builtins, the documents says:
Don't recognize built-in functions that do not begin with _builtin as prefix. See Other built-in functions provided by GCC, for details of the functions affected, including those which are not built-in functions when -ansi or -std options for strict ISO C conformance are used because they do not have an ISO standard meaning. [...]
回答2:
On some compiler standard library functions are enabled by default. On compiling
int main(){
printf("Hello World");
return 0;
}
this code on GCC 4.7.1 it compiles giving the warnings:
[Warning] implicit declaration of function 'printf' [-Wimplicit-function-declaration]
[Warning] incompatible implicit declaration of built-in function 'printf' [enabled by default]
来源:https://stackoverflow.com/questions/19230849/is-maths-library-included-in-the-glibc-now