Why fabs () does not require the -lm option when compiling with GCC

I wrote a simple program fabs.cto display the absolute value of a floating point number.

#include <stdio.h>
#include <math.h>

int main(void)
{
    float f;

    printf("Enter a floating-point number: ");
    scanf("%f", &f);

    printf("Its absolute value is %f.\n", fabs(f));
    return 0;
}
For function

fabs () is required to include a header file math.h, but I compiled without an option -lm.

  gcc fabs.c -o fabs

He even man fabssays link with -lm. But I do not know why I can compile it without -lm.

+4
source share
2 answers

, -lm, -lm. , , ( gcc). , . , ( fabs, ).

, , , " X", " A, B, C, , X, , , , , D B, , , A ( )".

-lm, , . , , , .

+3

gcc . printf, gcc fabs. , -fno-builtin, gcc:

yoones@laptop:/tmp/toto$ gcc -fno-builtin main.c 
/tmp/cc5fWozq.o: In function `main':
main.c:(.text+0x37): undefined reference to `fabs'
collect2: error: ld returned 1 exit status

nm :

yoones@laptop:/tmp/toto$ nm ./a.out 
0000000000600a18 B __bss_start
0000000000600a18 b completed.6661
0000000000600a08 D __data_start
0000000000600a08 W data_start
00000000004004b0 t deregister_tm_clones
0000000000400530 t __do_global_dtors_aux
00000000006007e8 t __do_global_dtors_aux_fini_array_entry
0000000000600a10 D __dso_handle
00000000006007f8 d _DYNAMIC
0000000000600a18 D _edata
0000000000600a20 B _end
0000000000400644 T _fini
0000000000400550 t frame_dummy
00000000006007e0 t __frame_dummy_init_array_entry
00000000004007d8 r __FRAME_END__
00000000006009d0 d _GLOBAL_OFFSET_TABLE_
                 w __gmon_start__
0000000000400408 T _init
00000000006007e8 t __init_array_end
00000000006007e0 t __init_array_start
0000000000400650 R _IO_stdin_used
                 U __isoc99_scanf@@GLIBC_2.7
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
00000000006007f0 d __JCR_END__
00000000006007f0 d __JCR_LIST__
                 w _Jv_RegisterClasses
0000000000400640 T __libc_csu_fini
00000000004005d0 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.2.5
0000000000400576 T main
                 U printf@@GLIBC_2.2.5
00000000004004f0 t register_tm_clones
0000000000400480 T _start
0000000000600a18 D __TMC_END__
+1

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


All Articles