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
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).
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)的输入,以便由链接器生产可执行文件。
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
#includethis 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).
#includesimple takes the header file and replaces the
#includeline 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)的输入,以便由链接器生产可执行文件。
相关文章推荐
- c或c++中都文件引用""与<>区别
- C/C++ 语言中.h文件和.c文件详细解析 引用 .c和.h文件的区别
- 解析C++普通局部变量与指针类型的对象变量的应用区别
- 解析C++普通局部变量与指针类型的对象变量的应用区别
- 请描述Java中异常处理机制的简单原理和应用,并说明Error与Exception有什么区别?
- SPI、I2C、UART三种串行总线的原理、区别及应用
- NIo、Bio、aio、 的原理及区别与应用场景
- C++ STL nth_element原理与应用
- 使用c头文件和c++头文件区别
- C++:源文件与头文件有什么区别
- why C++ 之: Why C++: 源文件与头文件有什么区别
- 装箱的原理,c++和C#装箱的一些区别
- C# 系统应用之调用SDelete程序粉碎文件及基础原理知识
- C语言主要应用领域,C和C++的区别,自上向下和自下向上设计思想
- C++中#include包含头文件带 .h 和不带 .h 的区别
- C++中#include包含头文件带 .h 和不带 .h 的区别
- Unix原理与应用学习笔记----第五章 普通文件1
- 向量的点乘、叉乘在Unity中的应用区别与原理
- SPI、I2C、UART三种串行总线的原理、区别及应用
- c++ 避免头文件重复引用 (pragma once与 #ifndef的区别)【转】