为什么要使用头文件,为什么不使用.lib文件 [英] Why to use header files and why not .lib files

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

问题描述


在C语言中,我们在程序顶部包含头文件.但是实际的逻辑是写在库中的.我的查询是为什么我们不能写:


In C we include header files in the top of the program. But the actual logic is written in the libraries. My query is why can''t we write:

#include<math.lib>



代替:



instead of:

#include<math.h>



据我所知,声明的函数对我没有用,除非并且直到我在.h文件中给它一个主体,我才拥有声明部分,但是实际的逻辑写在我的.lib文件中,所以为什么不包括lib文件直接.

请帮帮我
谢谢&问候
基数:rose:



As far as i know a declared function is of no use to me unless and until i give it a body in .h file I have the declaration part but the actual logic is written in my .lib file so why not to include the lib files directly.

Please help me out
Thanks & Regards
Radix :rose:

推荐答案

在C语言中,您不需要包含标头. C(和C ++)标头仅是方便的地方,用于使用Bung声明来定义特定代码块的接口.它们(应该)包含使用特定目标文件或库所需的最少信息.

假设我正在写一个世界.代替传统的方式,我可以这样写:

In C you don''t need to include headers. C (and C++) headers are just a handy place to bung declarations that define the interface to a particular lump of code. They (should) contain the minimum amount of information you need to use a particular object file or library.

Say I''m writing a hello world. Instead of the traditional way I can write:

int main( int, const char ** )
{
    int printf( const char *format, ... );
    printf( "Hello world!!\n" );
}



而且一切都会顺畅地进行(无论如何都是在标准实现上).

其背后的想法是使工具保持简单.要生成对printf的调用,编译器只需知道其接口是什么即可.如果编译器知道在设置参数后可以生成对函数的调用,并在函数返回时清理堆栈,则不必再了解printf的实现方式了.

它还使库和目标文件格式保持简单.在1970年代,编译/链接模型目标文件只是二进制数据的blob,其名称表映射到blob中的偏移量.如果您想要一个更复杂的模型,那么没有理由您不能实现一个模型,而是将更多的东西堆积到目标文件中,并最终得到诸如DLL或Java JAR之类的东西,而不是目标文件. br/>
因此,实际上有两个主要原因:

-惯性(或传统)-自1965年左右开始就存在(Fortran使用类似的模型)

-实用主义-可以更轻松地将各种东西链接在一起,包括汇编语言到高级语言.

干杯,

Ash



and it''ll all work swimmingly (on a standard implementation anyway).

The idea behind it is to keep the tools simple. To generate a call to printf the compiler just needs to know what its interface is. If the compiler knows that it can generate a call to the function after setting up the parameters and clean up the stack when the function returns it doesn''t have to know anymore about how printf is implemented.

It also keeps the library and object file format simple as well. With this 1970s compile/link model object files are just blobs of binary data with a table of names mapped to offsets into the blob. If you want a more sophisticated model there''s no reason you couldn''t implement one but you''d pile more stuff into the object file and end up with something like a DLL or Java JAR rather than an object file.

So there are really two main reasons:

- inertia (or tradition) - it''s been with us since 1965 or so (Fortran uses a similar model)

- pragmatism - makes it easier to link all sorts of stuff together, including assembly languages to high level languages.

Cheers,

Ash


头文件包含用C ++编写的声明,而.lib包含程序集.要求编译器解析程序集文件以进行符号检查不是很好.您了解编译器和链接器之间的区别吗?
Header file contain declarations written in C++ while .lib contains assembly. It is not very nice to ask compiler to parse assembly files for symbol checking. Do you understand the difference between compiler and linker?


这篇关于为什么要使用头文件,为什么不使用.lib文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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