什么是头文件和库文件? [英] What are Header Files and Library Files?

查看:462
本文介绍了什么是头文件和库文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/924485/whats-the-difference-between-a-header-file-and-a-library\">What's头文件和库?

谁能告诉我什么是头文件和库文件,它们的区别的实际意义?

Can anyone tell me what's the actual meaning of a header file and a library file and their difference?

例如我们包括在我们的计划.h扩展名和它只是定义,但实际执行中的库文件中定义,并在联阶段这样做的头文件,这是别人说什么,但有时我们包括库文件目录也为项目生成例如在POSIX线程的EXEC文件的人说,包括在命令行中-lpthread,但为什么当我们包含头文件#包括&LT;?>为什么我们仍然需要包括库文件也可我知道原因请??

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

推荐答案

一般情况下,头文件通知的编译器的某些东西(主要是其存在或声明),这样编译器可以正确地构建一个的单个转换单元(如单C文件)。

Generally, a header file notifies the compiler of certain things (mostly their existence or declarations) so that the compiler can correctly build a single translation unit (such as a single C file).

一个库文件是实际的执行code 的该做的工作是在头文件中指定。这是通过链接在链接的提供的实际功能(该_definitions而不仅仅是声明)。

A library file is the actual executable code that does the work as specified in that header file. This is linked in by the linker to provide the actual functionality (the _definitions rather than just the declarations).

所以,在你的榜样,你可能有一行:

So, in your example, you may have the line:

#include <pthread.h>

这告诉编译器的所有有关的存在的的 pthread_mutex_this pthread_condvar_that pthread_thread_the_other 的东西,但实际上并没有提供这些东西的实现。

which tells the compiler all about the existence of the pthread_mutex_this, pthread_condvar_that and pthread_thread_the_other stuff but doesn't actually provide the implementations of those things.

-lpthread 选项告诉它应该找到根据的pthread 名称的库链接器从它所用的实际实现拉,以福尔最后的可执行

The -lpthread option tells the linker that it should locate a library based on the pthread name from which it can pull in the actual implementations, in order to forn the final executable.

同样,尽管 stdio.h中保存有关I / O的东西的信息,实际code表示它会在运行时库(虽然你很少有该库专门链接,因为编译器会尽量照顾它给你)。因为通常与编译器链接(即,编译器调用你的连接器),它知道你可能会需要C运行时库。如果您是直接使用连接器(如使用 LD 命令),这可能不会发生,你必须要明确。

Similarly, while stdio.h holds 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 ld command), that probably wouldn't happen, and you'd have to be explicit.

这篇关于什么是头文件和库文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆