简体   繁体   中英

How the c program gets the definition of the library function , like printf

I have a simple program,

#include<stdio.h>
void main()
{
    printf(" Hello all \n");
}
  1. in the above program, how it gets the definition of the library function printf.
  2. as printf is a library function, so in our program how it is linked and when it is linked,,,, and how the compiler knows which library to link.
  3. what line in the above program indicate to the compiler to link librarys related to printf.
  1. The declaration is in stdio.h (just for type checking). The definition itself is in the C standard library ( libc.so on UNIX).
  2. The linker doesn't automatically know the library. However, it ALWAYS links against libc.so by default, so it always finds this definition. With other functions from other libraries, you have to figure out and supply the library yourself.
  3. None (see above).

in the above program, how it gets the definition of the library function printf.

The compiler can make this work any way it wants. The most common way is to actually have a file called stdio.h that has the definition.

as printf is a library function, so in our program how it is linked and when it is linked,,,, and how the compiler knows which library to link.

The compiler is either hard-coded with the knowledge of which library to link to or you have to tell it.

what line in the above program indicate to the compiler to link librarys related to printf.

That depends on the compiler. It could detect a #include <stdio.h> and link the library. It could always link the library. It could only link the library if you specifically ask it to. Check your compiler's documentation or ask about a specific compiler.

  1. "#include<stdio.h>" - contains the method signature.
  2. It doesn't. The linker does that. printf is in the Standard Library
  3. Nothing. The linker links. The compiler compiles. By default your linker almost certainly includes the Standard Library .

That's why it's called the standard library. 这就是为什么它被称为标准库。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM