从C中的另一个文件链接静态函数 [英] Linking static function from another file in c
问题描述
我有两个源文件:A.c和B.c
I have two source files: A.c and B.c
A.c有一个函数call_me:
A.c has a function, call_me:
static int call_me();
void call_me_register()
{
register_call_me(call_me);
}
如您所见,call_me函数用作变量,因此在A.o中具有call_me的符号
As you can see, call_me function is used as variable so it has a symbol of call_me in A.o
现在,我想在B.c文件中调用此call_me函数.
Now, I want to call this call_me function in B.c file.
static int call_me();
void call_call_me()
{
call_me();
}
如果我尝试链接B.o,则出现链接错误,即没有对call_me的引用.
If I try to link B.o, I've got an link error, that no such reference of call_me.
在这里,约束如下:由于某种原因,我无法修改A.c.在B.c的A.c中有没有叫"call_me"的电话?
Here, the constraint is the following: I can not modify A.c for some reason. Is there a any to call 'call_me' in A.c in B.c?
推荐答案
C中静态函数的可见性仅限于声明了它们的文件.因此,如果u在Ac中声明一个并从另一个文件中调用该函数,则bc您将收到一个链接错误,提示该函数未引用.希望此问题可以解决..
Visibility of Static functions in C is restricted to only the file in which they are declared.so if u declare one in A.c and call that function from another file b.c you will get a link error that the function is unreferenced..Hope this will solve your problem..
这篇关于从C中的另一个文件链接静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!