C:为什么我们要包含声明但未定义的头文件? [英] C: Why do we include header files, which declare but don't define?

查看:186
本文介绍了C:为什么我们要包含声明但未定义的头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在较高的层次上,我了解我们使用#include语句使其他文件中的代码可用于当前文件.但是我不明白为什么我们要包含一个头文件,其中包含声明但没有定义.

At a high level, I understand we use #include statements to make code from other files available to the current file. But I don't understand why we include a header file, which contains declarations but no definitions.

也许我需要学习更多有关编译/链接过程的知识,以充分理解其机制,但是一开始我是否有一个高层概念无法掌握?

Maybe I need to learn more about the compilation/linking process to fully understand the mechanics, but is there a high level concept I'm failing to grasp at the outset?

Edit :所有答案都有助于弄清我的问题,其归结为:一旦我们通知编译器某个函数在其他位置定义,它如何找出在哪里可以找到该定义?

Edit: All the answers helped clarify my question, which boils down to: once we've notified the compiler that a function is defined elsewhere, how does it figure out where to find that definition?

推荐答案

因为如果在不同的.c文件中包含有定义的标头,则会具有多个定义.

Because if you include the header where there are definitions, in different .c files, you will have Multiple Definitions.

声明是足够的,因为它允许编译器生成调用代码,然后链接程序负责查找定义并将函数调用链接到实际定义.

The declaration is sufficient, because it allows the compiler to generate the calling code, after that the linker takes care of finding the definition and links the function call to the actual definition.

这篇关于C:为什么我们要包含声明但未定义的头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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