[英]How to call the static function from another c file?
我想從另一個C文件調用靜態函數。 但是它始終顯示"function" used but never defined
。
在ble.c
static void bt_le_start_notification(void)
{
WPRINT_BT_APP_INFO(("bt_le_start_notification\n"));
}
在ble.h
static void bt_le_start_notification(void);
當我嘗試在main.c中調用bt_le_start_notification
時,它將顯示"bt_le_start_notification" used but never defined
。
在main.c中
#include "ble.h"
void application_start( void )
{
bt_le_start_notification();
}
我錯過了什么嗎? 提前致謝。
For restricting function access from other file, the keyword static is used
除聲明位置外,對靜態函數的訪問僅限於文件。當我們要限制對外部函數的訪問時,必須將其設置為靜態。 如果要從其他文件訪問函數,請使用全局函數,即非靜態函數。
我同意Frodo和ANBU.SANKAR。但是,如果要在文件外部調用靜態函數,則可以使用下面的示例。
1.c
extern (*func)();
int main(){
(func)();
return 0;}
2.c
static void call1(){
printf("a \n");
}
(*func)() = &call1;
靜態函數具有內部鏈接,並且只能由寫入同一文件的函數調用。 但是,如果要從另一個文件調用靜態函數,則可以使用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;
}
在main.c extern函數指針
#include "ble.h"
extern fn_ptr;
void application_start( void )
{
fn_ptr();
}
希望它會有用。
靜態功能范圍是在其中定義文件(即翻譯單元)的范圍。
之所以收到此消息,是因為您已將該函數聲明為靜態的。 因此,該實現僅在您的.c文件內部可見。 嘗試從.h和.c中刪除靜態變量,這應該可以看到您的函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.