简体   繁体   English

如何从另一个C文件调用静态函数?

[英]How to call the static function from another c file?

I want to call a static function from another C file. 我想从另一个C文件调用静态函数。 But it always show "function" used but never defined . 但是它始终显示"function" used but never defined

In ble.c 在ble.c

static void bt_le_start_notification(void)
{
    WPRINT_BT_APP_INFO(("bt_le_start_notification\n"));
}

In ble.h 在ble.h

static void bt_le_start_notification(void);

When I try to call bt_le_start_notification in main.c , it will show "bt_le_start_notification" used but never defined . 当我尝试在main.c中调用bt_le_start_notification时,它将显示"bt_le_start_notification" used but never defined

In main.c 在main.c中

#include "ble.h"

void application_start( void )
{
  bt_le_start_notification();
}

Did I missing something ? 我错过了什么吗? Thanks in advance. 提前致谢。

 For restricting function access from other file, the keyword static is used 

Access to static functions is restricted to the file except where they are declared.When we want to restrict access to functions from outer world, we have to make them static. 除声明位置外,对静态函数的访问仅限于文件。当我们要限制对外部函数的访问时,必须将其设置为静态。 If you want access functions from other file, then go for global function ie non static functions. 如果要从其他文件访问函数,请使用全局函数,即非静态函数。

I agree with Frodo and ANBU.SANKAR Still if you want to call a static function outside the file you can use examples like below. 我同意Frodo和ANBU.SANKAR。但是,如果要在文件外部调用静态函数,则可以使用下面的示例。

1.c 1.c

extern (*func)();
int main(){
(func)();
return 0;}

2.c 2.c

static void call1(){
printf("a \n");
}
(*func)() = &call1;

Static function has internal linkage and it can be called only by functions written in the same file. 静态函数具有内部链接,并且只能由写入同一文件的函数调用。 However if you want to call static function from another file , we have a trick in C. Fallow the steps. 但是,如果要从另一个文件调用静态函数,则可以使用C技巧。 1. Create a function pointer globally in ble.c and define it. 1.在ble.c中全局创建一个函数指针并定义它。

(void)(*fn_ptr)();
static void bt_le_start_notification(void)
{
    WPRINT_BT_APP_INFO(("bt_le_start_notification\n"));
    fn_ptr=bt_le_start_notification;

}

in main.c extern the function pointer 在main.c extern函数指针

 #include "ble.h"
extern fn_ptr;

void application_start( void )
{
  fn_ptr();
}

Hope it will be useful. 希望它会有用。

静态功能范围是在其中定义文件(即翻译单元)的范围。

The keyword static is often used to encapsulate the function in the source file where it is defined. 关键字static通常用于将函数封装在定义它的源文件中。 So it is not meant to call a static function from outside, an other c-file. 因此,这并不意味着要从外部调用另一个C文件的static函数。 An extern article[^] is explaining that topic pretty good I think. 我认为, 一篇外部文章[^]对该主题的解释很好。

Quote: 引用:

Static functions are much like private methods in Java or C++. 静态函数非常类似于Java或C ++中的私有方法。 A private method is a method which is only used by a class and can't be used outside of it. 私有方法是仅由类使用且不能在其外部使用的方法。 In C, we can declare a static function. 在C语言中,我们可以声明一个静态函数。 A static function is a function which can only be used within the source file it is declared in. 静态函数是只能在声明其的源文件中使用的函数。

So as a conclusion if you need to call the function from outside you do not define the function as static . 因此,可以断定,如果需要从外部调用函数,则不要将函数定义为static

You get this message because you have declared the function to be static. 之所以收到此消息,是因为您已将该函数声明为静态的。 So the implementation is only visible inside your .c file. 因此,该实现仅在您的.c文件内部可见。 Try removing the static from both your .h and .c, this should allow your function to be seen. 尝试从.h和.c中删除静态变量,这应该可以看到您的函数。

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

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