我想从另一个C文件调用静态函数.但它总是显示出来"function" used but never defined.
在ble.c
static void bt_le_start_notification(void)
{
WPRINT_BT_APP_INFO(("bt_le_start_notification\n"));
}
Run Code Online (Sandbox Code Playgroud)
在ble.h
static void bt_le_start_notification(void);
Run Code Online (Sandbox Code Playgroud)
当我尝试bt_le_start_notification在main.c中调用时,它将显示"bt_le_start_notification" used but never defined.
在main.c
#include "ble.h"
void application_start( void )
{
bt_le_start_notification();
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?提前致谢.
Anb*_*kar 18
For restricting function access from other file, the keyword static is used
Run Code Online (Sandbox Code Playgroud)
除了声明它们之外,对静态函数的访问仅限于文件.当我们想要限制从外部世界访问函数时,我们必须使它们成为静态的.如果你想从其他文件访问函数,那么去全局函数,即非静态函数.
sou*_*yar 13
我同意Frodo和ANBU.SANKAR如果你想在文件外调用静态函数,你可以使用下面的例子.
1.C
extern (*func)();
int main(){
(func)();
return 0;}
Run Code Online (Sandbox Code Playgroud)
2.C
static void call1(){
printf("a \n");
}
(*func)() = &call1;
Run Code Online (Sandbox Code Playgroud)
小智 6
静态函数具有内部链接,并且只能由写入同一文件的函数调用。但是,如果要从另一个文件调用静态函数,则可以使用C技巧。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;
}
Run Code Online (Sandbox Code Playgroud)
在main.c extern函数指针
#include "ble.h"
extern fn_ptr;
void application_start( void )
{
fn_ptr();
}
Run Code Online (Sandbox Code Playgroud)
希望它会有用。