简体   繁体   English

头文件和库之间有什么区别?

[英]What's the difference between a header file and a library?

One of the things I'm having a hard time understanding is how the compiler works. 我很难理解的一件事是编译器是如何工作的。 I'm having a lot of difficulties with it, but in particular I keep getting headers and libraries mixed up. 我遇到了很多困难,但特别是我总是把标题和库混在一起。 If somebody could clear things up a bit, that'd be great. 如果有人能稍微清楚一点,那就太好了。

Think of both like this (Disclaimer: this is a really high-level analogy ;) .. 想想这两个(免责声明:这是一个非常高级的类比;)..

  • The header is a phone number you can call, while... 标题是您可以拨打的电话号码,而...
  • ...the library is the actual person you can reach there! ... 图书馆是你可以到达那里的真实人物!

It's the fundamental difference between "interface" and "implementation"; 这是“界面”和“实施”之间的根本区别; the interface (header) tells you how to call some functionality (without knowing how it works), while the implementation (library) is the actual functionality. 接口 (header)告诉你如何调用某些功能(不知道它是如何工作的),而实现 (库)是实际的功能。

Note: The concept is so fundamental, because it allows you flexibility: you can have the same header for different libraries (ie the functionality is exactly called in the same way), and each library may implement the functionality in a different way. 注意:这个概念是如此基础,因为它允许您灵活性:您可以为不同的库具有相同的头(即,以相同的方式完全调用功能),并且每个库可以以不同的方式实现功能。 By keeping the same interface, you can replace the libraries without changing your code. 通过保持相同的接口,可以更换库不改变你的代码。

And: you can change the implementation of the library without breaking the calling code! 并且:您可以在不破坏调用代码的情况下更改库的实现!

A header file is generally used to define an interface or set of interfaces within an application. 头文件 通常用于定义应用程序中的接口或接口集。 Think of a header file as something which shows the external functionality of a program while omitting the technical implementation details. 将头文件视为显示程序外部功能的内容,同时省略技术实现细节。

For example, if you were optimising a program, you would most likely modify the source (.cpp) file to improve the algorithm, but the header file wouldn't change, because external clients still call the methods using the same set of parameters and return values. 例如,如果您正在优化程序,则很可能会修改源(.cpp)文件以改进算法,但是头文件不会更改,因为外部客户端仍使用相同的参数集调用方法,返回值。

In an object-oriented language like C++, a header file generally includes the following: 在像C ++这样的面向对象语言中,头文件通常包括以下内容:

  • Class description and inheritance hierarchy 类描述和继承层次结构
  • Class data members and types 类数据成员和类型
  • Class methods 类方法

While there is nothing stopping code from being implemented in a header file, this is generally not favoured as it can introduce extra coupling and dependencies in the code. 虽然没有什么可以阻止代码在头文件中实现,但这通常不受欢迎,因为它可以在代码中引入额外的耦合和依赖。

In some cases (eg templated classes) the implementation must be defined in the header file for technical reasons. 在某些情况下(例如模板化类),由于技术原因, 必须在头文件中定义实现。


A library is a collection of code which you want to make available to a program or group of programs. 是您希望提供给程序或程序组的代码集合。 It includes the implementation of a particular interface or set of interfaces. 它包括一个特定接口实现或一组接口。

Code is defined in a library to prevent code duplication and encourage re-use. 代码在库中定义,以防止代码重复并鼓励重用。 A library can be statically-linked (.lib) or dynamically-linked (.dll): 库可以是静态链接 (.lib)或动态链接 (.dll):

  • A statically-linked library defines a set of export symbols (which can be thought of as method definitions) which are then linked into the final executable (.exe) during the linking stage of the build process. 静态链接库定义了一组导出符号(可以将其视为方法定义),然后在构建过程的链接阶段将其链接到最终可执行文件(.exe)。 It has the advantage of faster execution time (as the library doesn't need to be dynamically loaded), at the expense of a larger binary (because the methods are essentially replicated in the executable file). 它具有更快的执行时间(因为库不需要动态加载)的优点,代价是更大的二进制文件(因为这些方法基本上在可执行文件中复制)。

  • A dynamically-linked library is linked during the execution of a program, rather than the linking of a program. 动态链接库在程序执行期间链接,而不是链接程序。 It is useful when multiple programs need to re-use the same methods, and is used extensively in technologies such as COM. 当多个程序需要重用相同的方法时,它很有用,并广泛用于COM等技术。

One thing that may be confusing you is that the word library can have several meanings in C++. 可能让您感到困惑的一件事是,单词在C ++中可以有多种含义。 One meaning has been well-discussed here: 这里有一个很好的讨论:

A linkable set of functions in a binary file. 二进制文件中的一组可链接函数。 These can be statically linked or dynamically linked. 这些可以静态链接或动态链接。

But there is another type of library: so-called header-only libraries (including parts of STL, TR1 and Boost). 但是还有另一种类型的库:所谓的标题库(包括STL,TR1和Boost的部分)。 These do not exist in a separate binary form so the word library does not refer to a particular binary file but rather to a set of included header files. 这些不存在于单独的二进制形式中,因此单词不是指特定的二进制文件,而是指一组包含的头文件。

Hope this helps. 希望这可以帮助。

A library is code, compiled into a set of object files. 库是代码,编译成一组目标文件。 The object files contain the compiled machine code and the data declarations used by the code. 目标文件包含已编译的机器代码和代码使用的数据声明。

A header file defines the interface to a library: it tells you how to use the library correctly. 头文件定义了库的接口 :它告诉您如何正确使用库。 In C/C++, a header file gives you a list of function names and how to call those functions: the number and types of parameters they take, the return type, the calling convention, etc. Header files have a lot of other stuff in them, too, but in the end, what it boils down is a set of rules for calling library code. 在C / C ++中,头文件为您提供了函数名列表以及如何调用这些函数:它们采用的参数的数量和类型,返回类型,调用约定等。头文件中还有很多其他内容它们也是如此,但最终,它归结为一组调用库代码的规则。

标头只包含声明,而库也包含实现。

If Library in programming languages is a general library , then many books present in the library can be compared with functions/methods in languages . 如果编程语言中的库是一般库,则库中存在的许多书籍可以与语言中的函数/方法进行比较。 And also header files can be compared with the row number of the book Suppose there is a book in some library in Hyderabad and in that library , that book is present in row Number 24 ... The same way the address of the library is given by using the namespace std (for standard library) and row No is given by header file where all the books(methods in this case) of same time(all methods related to input/output streams) are put up 并且头文件也可以与书的行号进行比较假设在海得拉巴的一些图书馆和那个图书馆里有一本书,那本书出现在第24行...同样的方式给出了图书馆的地址通过使用命名空间std(用于标准库)和行No,由头文件给出,其中所有书籍(在这种情况下为方法)同时(与输入/输出流相关的所有方法)都被提起

HEADER FILE is that in which declaration of a function is written.By using header file we can access a particular function HEADER FILE是写入函数声明的函数。通过使用头文件,我们可以访问特定的函数

while

LIBRARY FILE is that in which definition of a particular function is written. LIBRARY FILE是写入特定函数的定义的文件。 MATH.H is a HEADER FILE while MATH.LIB is library file. MATH.H是HEADER FILE,而MATH.LIB是库文件。

Working of HEADER File and LIBRARY in a Program. 在程序中使用HEADER文件和LIBRARY。

A header file contains the links to libraries (libraries contain standard functions and methods), a compiler recognizes the standard functions used in the source code via a preprocessor, that resolves all the directives(directives are the lines in program preceded by # sign that include ) before the actual compilation of the program. 头文件包含指向的链接(库包含标准函数和方法),编译器通过预处理器识别源代码中使用的标准函数,它解析所有指令(指令是程序中以#符号开头的行) )在实际编制程序之前。

Thanks for reading! 谢谢阅读!

I think library as a package of code which is reused many times and and that code is precompiled , hence it is available in standard form so that we do not have to write that code for every program that we develop . 我认为库是一个代码包,可以多次重复使用,而且代码是预编译的,因此它以标准形式提供,这样我们就不必为我们开发的每个程序编写代码。 And header file contain the reference to that code in simple way the functions we use in our program like "cin" and "cout" are fully defined in a standard library ,and header files like iostream header file contains the reference to that code. 头文件以简单的方式包含对该代码的引用我们在程序中使用的函数如“cin”和“cout”在标准库中完全定义,而像iostream头文件这样的头文件包含对该代码的引用。 So when we compile our code it we just get the precompiled for cin and cout, and we do not have to write the code for cin and cout for every time we use it. 因此,当我们编译代码时,我们只需要为cin和cout预编译,并且我们不必每次使用它时都为cin和cout编写代码。 Or In a more simple way we can say that a library contain codes for all the functions and a header file is way to reach that code. 或者以更简单的方式,我们可以说库包含所有函数的代码,而头文件是达到该代码的方式。

A library is a collection of similar objects for occasional use . 库是偶尔使用的类似对象的集合。 It usually contains programmes in object or source code form, templates, etc. 它通常包含对象或源代码形式的程序,模板等。

A header file is the location (interface) of the library 头文件是库的位置(接口)

头文件描述了如何调用该功能 ,库包含实现此功能的编译代码。

Libraries are like dead mummies,wrapped in white long threads. 图书馆就像死去的木乃伊,用白色的长线包裹着。 They are dead. 他们死了。 Only way to release them is through header files. 释放它们的唯一方法是通过头文件。 Header files contain ways to bring them to life and they can be brought to life many times(code reuse). 头文件包含使它们生动的方法,它们可以多次使用(代码重用)。

You can consider this example to understand- Math.h is a header file which includes the prototype for function calls like sqrt() , pow() etc, whereas libm.lib , libmmd.lib , libmmd.dll are some of the math libraries. 您可以考虑这个例子来理解 - Math.h是一个头文件,其中包含函数调用的原型,如sqrt()pow()等,而libm.liblibmmd.liblibmmd.dll是一些数学库。 In simple terms a header file is like a visiting card and libraries are like a real person, so we use visiting card(Header file) to reach to the actual person(Library). 简单来说,头文件就像一张名片,图书馆就像一个真人,所以我们使用名片(头文件)来联系真人(图书馆)。

Header is typically used to contain the prototypes. 标头通常用于包含原型。 Headers expand at pre-processing time so that at compile time, the code may have access to relevant function declarations/ prototypes. 标头在预处理时间扩展,以便在编译时,代码可以访问相关的函数声明/原型。

Library is the actual software that contains the definitions of the function prototypes (present in the header). 库是包含函数原型定义的实际软件(存在于头文件中)。 Library is used at link time. 库在链接时使用。 The definitions (present in library) are resolved at link time. 定义(存在于库中)在链接时解析。

用一个经典笑话来解释,不同之处在于库有头文件而头文件没有库。

Code from libraries will only be stored as needed for a header file. 库中的代码只会根据头文件的需要进行存储。 The whole header file will be stored, which saves processor storage area. 将存储整个头文件,这节省了处理器存储区域。

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

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