在 C 中使用 extern 来表示函数
如果我们有 2 个同名的源文件和 1 个头文件,例如
example.c,其中包含 example.h,内容如上所示:
example.c
#include "example.h"
unsigned int add(unsigned int, unsigned int);
unsigned int add(unsigned int number1, unsigned int number2)
{
return number1+number2;
}
example2.c
#include "example.h"
int main(void)
{
add(2, 3);
}
example.h
#define SHIFT_LEFT_4(x) x*16
#define SHIFT_LEFT_3(x) x*8
#define SHIFT_LEFT_2(x) x*4
extern unsigned int add(unsigned int, unsigned int);
中可以有两个 add 函数声明吗? example.c,一个来自 example.h 文件作为 extern,一个来自源文件内部,编译器将如何看待它?这是以不同的方式进行还是那样可以?我知道函数默认是外部的,我编写它只是为了更容易地解释问题。
If we have 2 source file and 1 header file of the same name for example
example.c which includes example.h and content is like above:
example.c
#include "example.h"
unsigned int add(unsigned int, unsigned int);
unsigned int add(unsigned int number1, unsigned int number2)
{
return number1+number2;
}
example2.c
#include "example.h"
int main(void)
{
add(2, 3);
}
example.h
#define SHIFT_LEFT_4(x) x*16
#define SHIFT_LEFT_3(x) x*8
#define SHIFT_LEFT_2(x) x*4
extern unsigned int add(unsigned int, unsigned int);
Is it okay to have two declarations of add function in example.c, one coming from example.h file as extern and one inside the source file, how the compiler will look at it? Is this done differently or that way is fine? I know that functions are extern by default, I just wrote it to explain easier the question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论