简体   繁体   English

从C中的另一个文件链接静态函数

[英]Linking static function from another file in c

I have two source files: Ac and Bc 我有两个源文件:Ac和Bc

Ac has a function, call_me: Ac具有一个函数call_me:

static int call_me();

void call_me_register()
{
    register_call_me(call_me);
}

As you can see, call_me function is used as variable so it has a symbol of call_me in Ao 如您所见,call_me函数用作变量,因此在Ao中具有call_me的符号

Now, I want to call this call_me function in Bc file. 现在,我想在Bc文件中调用此call_me函数。

static int call_me();

void call_call_me()
{
    call_me();
}

If I try to link Bo, I've got an link error, that no such reference of call_me. 如果我尝试链接Bo,则出现链接错误,即没有call_me这样的引用。

Here, the constraint is the following: I can not modify Ac for some reason. 在这里,约束如下:由于某种原因,我无法修改Ac。 Is there a any to call 'call_me' in Ac in Bc? 在Bc中的Ac中有没有可以调用“ call_me”的电话?

The register function in A.cpp will presumably store a pointer to the function somewhere (hence that is the registration) and you can use that pointer to call the function. A.cpp中的register函数可能会将指向该函数的指针存储在某个位置(因此就是注册),您可以使用该指针来调用该函数。 -- so looks for a function to fetch the registered call_me function. -因此寻找一个函数来获取已注册的call_me函数。

You cannot reference the function directly -- as it is static -- and being declared static as in you sample code means that the function is hidden for the outside world. 您不能直接引用该函数-因为它是静态的-并且如您的示例代码中一样声明为静态意味着该函数对于外界是隐藏的。

静态函数在C语言中的可见性仅限于声明它们的文件。因此,如果您在Ac中声明一个静态函数并从另一个文件中调用该函数,则您会得到一个链接错误,指出该函数未被引用。希望此方法可以解决你的问题..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM