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

后端 未结 11 1216
难免孤独
难免孤独 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 02:10

    If I put stdlib.h or stdio.h, I don't have to link those but I have to link when I compile:

    stdlib.h, stdio.h are the header files. You include them for your convenience. They only forecast what symbols will become available if you link in the proper library. The implementations are in the library files, that's where the functions really live.

    Including math.h is only the first step to gaining access to all the math functions.

    Also, you don't have to link against libm if you don't use it's functions, even if you do a #include which is only an informational step for you, for the compiler about the symbols.

    stdlib.h, stdio.h refer to functions available in libc, which happens to be always linked in so that the user doesn't have to do it himself.

提交回复
热议问题