简体   繁体   English

C程序如何获取库函数的定义,例如printf

[英]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. 在上面的程序中,它如何获取库函数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. 因为printf是一个库函数,所以在我们的程序中它是如何链接的以及何时链接、、以及编译器如何知道要链接的库。
  3. what line in the above program indicate to the compiler to link librarys related to printf. 上面程序中的哪行指示编译器链接与printf相关的库。
  1. The declaration is in stdio.h (just for type checking). 该声明在stdio.h (仅用于类型检查)。 The definition itself is in the C standard library ( libc.so on UNIX). 定义本身在C标准库中(在UNIX上为libc.so )。
  2. The linker doesn't automatically know the library. 链接器不会自动知道该库。 However, it ALWAYS links against libc.so by default, so it always finds this definition. 但是,默认情况下,它始终与libc.so链接,因此它始终会找到此定义。 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. 在上面的程序中,它如何获取库函数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. 最常见的方法是实际拥有一个具有定义的名为stdio.h的文件。

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. 因为printf是一个库函数,所以在我们的程序中它是如何链接的以及何时链接、、以及编译器如何知道要链接的库。

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. 上面程序中的哪行指示编译器链接与printf相关的库。

That depends on the compiler. 那取决于编译器。 It could detect a #include <stdio.h> and link the library. 它可以检测到#include <stdio.h>并链接库。 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. “ #include <stdio.h>”-包含方法签名。
  2. It doesn't. 没有。 The linker does that. 链接器执行此操作。 printf is in the Standard Library printf在标准库中
  3. Nothing. 没有。 The linker links. 链接器链接。 The compiler compiles. 编译器进行编译。 By default your linker almost certainly includes the Standard Library a . 默认情况下,链接程序几乎可以肯定包含标准库a

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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