C语言 什么是头文件和库文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6407975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are Header Files and Library Files?
提问by karthik
Possible Duplicate:
What's the difference between a header file and a library?
可能的重复:
头文件和库之间有什么区别?
Can anyone tell me what's the actual meaning of a header file and a library file and their difference?
谁能告诉我头文件和库文件的实际含义及其区别是什么?
For example we include header file with .h extension in our program and its just the definition but the actual implementation is defined in library files and this is done at linking stage this is what people say but sometimes we include the library files directory too for the programs to generate the exec file for example in posix threads people say to include the -lpthread in the command line but why when we included the header file #include<> why we still need to include the library files too may i know the reason please??
例如,我们在程序中包含带有 .h 扩展名的头文件,它只是定义,但实际实现是在库文件中定义的,这是在链接阶段完成的,这是人们所说的,但有时我们也包含库文件目录例如在posix线程中生成exec文件的程序人们说在命令行中包含-lpthread但是为什么当我们包含头文件#include<>为什么我们仍然需要包含库文件我可以知道原因吗??
回答by paxdiablo
Generally, a header file notifies the compilerof certain things (mostly their existence or declarations) so that the compiler can correctly build a single translation unit (such as a single C file).
通常,头文件会通知编译器某些事情(主要是它们的存在或声明),以便编译器可以正确构建单个翻译单元(例如单个 C 文件)。
A library file is the actual executable codethat does the work as specified in that header file. This is linked in by the linkerto provide the actual functionality (the _definitions rather than just the declarations).
库文件是执行该头文件中指定的工作的实际可执行代码。这由链接器链接以提供实际功能(_定义而不仅仅是声明)。
So, in your example, you may have the line:
因此,在您的示例中,您可能有以下行:
#include <pthread.h>
which tells the compiler all about the existenceof the pthread_mutex_this, pthread_condvar_thatand pthread_thread_the_otherstuff but doesn't actually provide the implementations of those things.
它告诉编译器所有关于,和东西的存在,但实际上并没有提供这些东西的实现。pthread_mutex_thispthread_condvar_thatpthread_thread_the_other
The -lpthreadoption tells the linker that it should locate a library based on the pthreadname from which it can pull in the actual implementations, in order to forn the final executable.
该-lpthread选项告诉链接器它应该根据pthread它可以从中拉入实际实现的名称来定位一个库,以便形成最终的可执行文件。
Similarly, while stdio.hholds information about the I/O stuff, the actual code for it will be in the runtime library (though you rarely have to link that library specifically since the compiler will try to take care of it for you). Because you usually link with the compiler (i.e., the compiler invokes the linker for you), it knows that you're probably going to need the C run time library. If you were to use the linker directly (such as by using the ldcommand), that probably wouldn't happen, and you'd have to be explicit.
类似地,虽然stdio.h保存有关 I/O 内容的信息,但它的实际代码将在运行时库中(尽管您很少需要专门链接该库,因为编译器会尝试为您处理它)。因为您通常与编译器链接(即,编译器为您调用链接器),所以它知道您可能需要 C 运行时库。如果您要直接使用链接器(例如通过使用ld命令),那可能不会发生,并且您必须明确。
回答by Pankaj Kumar
Header Files:These are the files that are included at the top of any program. If we use any function inside a program, then the header file containing declaration or definition of that function ,has to be included.Like printf() is defined in stdio.h.So, we must include it (by #include in order to use printf().
头文件:这些是包含在任何程序顶部的文件。如果我们在程序中使用任何函数,那么必须包含包含该函数声明或定义的头文件。就像在 stdio.h 中定义 printf() 一样,我们必须包含它(通过 #include 以使用 printf()。
Library Files:These are the files which the compiler uses in order to define the functions which have been used in the program and had been declared inside the header file.Like, printf() has its complete definition ,like how it will work etc. in an I/O library! So, the compiler uses that library to get the machine code for printf.
库文件:这些是编译器用来定义程序中使用的函数并在头文件中声明的函数的文件。比如,printf() 有它的完整定义,比如它将如何工作等。在 I/O 库中!因此,编译器使用该库来获取 printf 的机器代码。
Difference:
区别:
- Header files are TEXT files while library files are BINARY. This means, we can read and modify the header file but not the library!
- Header file is in C language while the library is in machine language!
- Header file has to be included by the programmer while the compiler automatically relates the library file(s) with the program!
- 头文件是 TEXT 文件,而库文件是 BINARY。这意味着,我们可以读取和修改头文件,但不能读取和修改库!
- 头文件是C语言的,而库是机器语言的!
- 程序员必须包含头文件,而编译器会自动将库文件与程序相关联!
回答by Ozair Kafray
The header files only include the definition of the functions that you would use in a file where the header file is being included.
头文件仅包含您将在包含头文件的文件中使用的函数的定义。
Library files comprise the actual implementation of the functions that you will be using in your program.
库文件包含您将在程序中使用的函数的实际实现。
The header file is included (copy/pasted) during the preprocessing stage and is compiled as part of the program being written during compilation phase. One has to specify the -lpthread in the command line, so that the linker will know which libraryto look into for functions used in the program.
头文件在预处理阶段被包含(复制/粘贴),并作为在编译阶段编写的程序的一部分进行编译。必须在命令行中指定 -lpthread,以便链接器知道要查找程序中使用的函数的库。
Similar Question/Answer on Stackoverflow explaining it in layman terms:
Stackoverflow 上的类似问题/答案用外行术语解释它:
What's the difference between a header file and a library?
Part 2: Why we do not have to alwaysinclude library files when we have #include?
第 2 部分:为什么我们不必总是包含库文件#include?
This might be the case when:
在以下情况下可能是这种情况:
i. The implementation of the functions is included in the header file.
ii. The implementation of the functions is in
cfiles for which you have the source available.iii. The required libraries are included by your compiler by defaulte.g., standard c libraries.
一世。函数的实现包含在头文件中。
ii. 函数的实现位于
c您有可用源的文件中。
NOTE: Here is a reference to what is included in the standard C library, which is included by default by many compilers.
注意:这是对标准 C 库中包含的内容的引用,许多编译器默认包含该库。

