Why do you have to link the math library in C?

后端 未结 11 1089
难免孤独
难免孤独 2020-11-22 01:04

If I include or in a C program I don\'t have to link these when compiling but I do have to link to

11条回答
  •  逝去的感伤
    2020-11-22 01:53

    As ephemient said, the C library libc is linked by default and this library contains the implementations of stdlib.h, stdio.h and several other standard header files. Just to add to it, according to "An Introduction to GCC" the linker command for a basic "Hello World" program in C is as below:

    ld -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o 
    /usr/lib/crti.o /usr/libgcc-lib /i686/3.3.1/crtbegin.o
    -L/usr/lib/gcc-lib/i686/3.3.1 hello.o -lgcc -lgcc_eh -lc 
    -lgcc -lgcc_eh /usr/lib/gcc-lib/i686/3.3.1/crtend.o /usr/lib/crtn.o
    

    Notice the option -lc in the third line that links the C library.

提交回复
热议问题