您的位置:首页 > 编程语言 > C语言/C++

C/C++中头文件和库文件的原理、区别及应用

2013-12-25 10:09 363 查看
 Librariesare collections of precompiled functions that have been written to be reusable.   Typically, they consist of sets of related functions to
perform a common task. 

Firstly, think of both like this (a analogy) :
The header is a phone number u can call, while ...
The library is the actual person u can reach there.
It's the similar difference between "interface" and "implementation"; theinterface (header) tells youhow to call some functionality (without knowing how it works), while theimplementation
(library) is the actual functionality.
Advantages:

1. It allows your flexibility: you can have the same header for different libraries (i.e. the functionality is exactly called in the same way), and each library mayimplement the functionality in a different way.
By keeping the same interface, you can replace/revise the libraries without changingyour calling codes.
2. By putting each function in it's own .c file, you can reduce your overall build times by only recompiling the files thatneed to be recompiled.
3. Avoid linker errors: You can't put functiondefinition in the header files. If later you would
#include
this header in to more then one c file, both of them would compile the function implementation
into two separate object files. The compiler would work well, but when the linker wanted to link together everything, it would findtwo implementation of the function and would give you an error.

 
1. 在文件名为util.c的文件中实现一个函数功能,编译该文件并生成对象文件(object files). 该生成文件即可以作为库文件(Library file)使用了

#gcc -c util.c
#ls *.o
util.o

2. 因为头文件有以上好处,所以最好为刚才生产库文件写一个头文件,这个头文件声明了你的库文件中实现了哪些功能。如果某程序想要用库函数,把该头文件写入该程序。

Header (my_header.h):

Source file (Program.c):

3.  When you compile C code, the compiler has to actually know that a particular function exists with a defined name, parameter list, return type and optional modifiers. All these things are called function signature and the existence
of a particular function is declared in the header file. Having this information, when the compiler finds a call to this function, it will know which type of parameters to look for, can control whether they have the appropriate type and prepare them
to a structure that will be pushed to the stack before the code actually jumps to your function implementation. However the compiler does not have to know the actual implementation of the function, it simple puts a "placeholder" in your object file to all
function calls. (Note: each c files compiles to exactly one object file).
#include
simple takes the header file and replaces the
#include
line with the contents of the file. When you go to compile this, the preprocessor first takes care of its business and generates an intermediate file with the contents of the
header pasted into the source file as such:

Code:

 

4. After the compilation the build script passes all object files to thelinker. The linker will be that resolves all function "placeholders" finding the physical location of the function implementation,
let them be among your object files, framework libraries or dlls. It simple places the information where the function implementation can be found to all function calls thus your program will know where to continue execution when it arrives to your function
call.
现在,可以链接、运行了

$gcc -c program.c    
# 源程序对象文件和库对象文件进行链接
$gcc -o program program.o util.o 
$./program

 
 注: 对象文件(object file)是可重载的机器语言代码,不能直接运行,是作为链接器(linker)的输入,以便由链接器生产可执行文件。
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: