链接器如何知道extern函数的定义在哪里? [英] How does the linker know where is the definition of an extern function?

查看:909
本文介绍了链接器如何知道extern函数的定义在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了几个帖子,并得出结论,extern告诉编译器这个函数存在,但它的代码在别的地方,不要惊慌。但是链接器如何知道函数的定义位置。

I read a few posts and concluded that extern tells compiler that "This function exists, but the code for it is somewhere else. Don't panic." But how does the linker know where the function is defined.

我的CASE: -
我正在使用Keil uvision 4.有一个头文件grlib。 h,主函数在grlib_demo.c中(它包括grlib.h)。现在,有一个函数GrCircleDraw()在Circle.c中定义并在grlib_demo.c中调用,还有一个语句

My CASE:- I am working on Keil uvision 4. There is a header file grlib.h and the main function is in grlib_demo.c(it includes grlib.h). Now, there is a function GrCircleDraw() which is defined in Circle.c and called in grlib_demo.c, there is also a statement


extern void GrCircleDraw(all arguments);

extern void GrCircleDraw(all arguments);

在grlib.h中。我的查询是链接器知道GrCircleDraw()的定义在哪里,因为Circle.c不包括在grlib.h和grlib_demo.c

in grlib.h. My query is how linker knows where the definition of GrCircleDraw() is since Circle.c is not included in grlib.h and grlib_demo.c

注意: - 文件grlib .h和Circle.c在同一个文件夹中。代码运行成功。

Note :- The files grlib.h and Circle.c are in the same folder. The code runs successfully.

推荐答案

简单的回答是编译器不需要知道,能够找到它。通过多个 .o 文件,或者通过库,链接器必须能够找到 GrCircleDraw 函数。

The simple answer is that "the compiler doesn't need to know, but the linker has to be able to find it". Through multiple .o files, or through libraries, the linker has to be able to find a single definition of the GrCircleDraw function.

这篇关于链接器如何知道extern函数的定义在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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