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

C/C++的库文件包含预编译的可重用函数,而头文件则提供调用接口。头文件类似于电话号码,定义了如何调用功能,而库文件则是实际实现功能的部分。这种分离提供了灵活性,允许使用相同的接口替换不同实现的库,减少了编译时间,并避免链接错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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):
void foo(int);
int bar(char*,int);
Source file (Program.c):
#include "my_header.h"
 
void my_function()
{
    intblah = 10;
    chararray[8];
   /* Call other functions */
    foo(blah);
}
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:
void foo(int);
int bar(char*,int);
 
void my_function()
{
    intblah = 10;
    chararray[8];
   /* Call other functions */
    foo(blah);
    bar(array,sizeof(array));
}
 
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)的输入,以便由链接器生产可执行文件。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值