C++ 头文件和库有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/924485/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:59:19  来源:igfitidea点击:

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

c++c

提问by Xonara

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.

我很难理解的一件事是编译器是如何工作的。我遇到了很多困难,但特别是我总是把头文件和库弄混了。如果有人能把事情弄清楚一点,那就太好了。

回答by beef2k

Think of both like this (Disclaimer: this is a really high-level analogy ;) ..

可以这样想(免责声明:这是一个非常高级的类比;)..

  • The headeris a phone number you can call, while...
  • ...the libraryis the actual person you can reach there!
  • 标题是您可以拨打一个电话号码,而...
  • ...图书馆是您可以到达那里的实际人!

It's the fundamental difference between "interface" and "implementation"; the interface(header) tells you howto call some functionality (without knowing how it works), while the implementation(library) is the actual functionality.

这是“接口”和“实现”之间的根本区别;在接口(头),告诉你如何调用一些功能(不知道它是如何工作),而实现(库)是实际的功能。

Note: The concept is so fundamental, because it allows you flexibility: you can have the same header for different libraries (i.e. the functionality is exactly called in the same way), and each library may implementthe functionality in a different way. By keeping the same interface, you can replace the libraries without changing yourcode.

注意:这个概念非常基础,因为它允许您灵活使用:您可以为不同的库使用相同的头文件(即功能完全以相同的方式调用),并且每个库可以以不同的方式实现功能。通过保持相同的接口,可以更换库不改变你的代码。

And: you can change the implementation of the library without breaking the calling code!

并且:您可以在不破坏调用代码的情况下更改库的实现!

回答by LeopardSkinPillBoxHat

A header fileis generallyused 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 (e.g. templated classes) the implementation mustbe defined in the header file for technical reasons.

在某些情况下(例如模板化类),出于技术原因,必须在头文件中定义实现。



A libraryis a collection of code which you want to make available to a program or group of programs. It includes the implementationof a particular interfaceor 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-linkedlibrary 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. 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-linkedlibrary 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.

  • 静态链接库定义了一组然后被在生成过程中的连接阶段链接到最后的可执行文件(.exe)导出符号(其可以被认为是方法定义)的。它的优点是执行时间更快(因为库不需要动态加载),但代价是更大的二进制文件(因为方法本质上是在可执行文件中复制的)。

  • 动态链接库中的程序的执行,而不是节目的链接过程中连接。当多个程序需要重用相同的方法时,它很有用,并广泛用于 COM 等技术中。

回答by jwfearn

One thing that may be confusing you is that the word librarycan have several meanings in C++. One meaning has been well-discussed here:

可能会让您感到困惑的一件事是,一词在 C++ 中可能有多种含义。一种含义在这里得到了很好的讨论:

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). These do not exist in a separate binary form so the word librarydoes not refer to a particular binary file but rather to a set of included header files.

但是还有另一种类型的库:所谓的仅头文件库(包括 STL、TR1 和 Boost 的一部分)。这些并不在一个单独的二进制形式存在,因此这个词并不是指一个特定的二进制文件,而是一组包含的头文件。

Hope this helps.

希望这可以帮助。

回答by Adam Rosenfield

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 interfaceto 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++ 中,头文件为您提供了一个函数名称列表以及如何调用这些函数:它们采用的参数数量和类型、返回类型、调用约定等。头文件中有很多其他内容它们也是,但归根结底是一套调用库代码的规则。

回答by Moayad Mardini

Header only contains the declaration, whereas libraries also contains the implementaion.

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

回答by user2131930

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 由头文件给出,其中放置了所有相同时间(与输入/输出流相关的所有方法)的书籍(在这种情况下的方法)

回答by vipin

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. MATH.H is a HEADER FILE while MATH.LIB is library file.

LIBRARY FILE 是写入特定函数定义的文件。MATH.H 是头文件,而 MATH.LIB 是库文件。

回答by Nauman Khan

Working of HEADER File and LIBRARY in a Program.

在程序中处理 HEADER 文件和 LIBRARY。

A header filecontains 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!

谢谢阅读!

回答by shivam

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. 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. 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.

我认为库是一个可重复使用多次的代码包,并且该代码是预编译的,因此它以标准形式提供,因此我们不必为我们开发的每个程序编写该代码。头文件以简单的方式包含对该代码的引用,我们在程序中使用的函数(如“cin”和“cout”)完全定义在标准库中,而头文件(如 iostream 头文件)包含对该代码的引用。所以当我们编译我们的代码时,我们只得到 cin 和 cout 的预编译,我们不必每次使用 cin 和 cout 编写代码。或者以更简单的方式,我们可以说一个库包含所有函数的代码,而头文件是访问该代码的方式。

回答by Jayraj Srikriti Naidu

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

头文件是库的位置(接口)