I want to call a static function from another C file. But it always show "function" used but never defined
.
In ble.c
static void bt_le_start_notification(void)
{
WPRINT_BT_APP_INFO(("bt_le_start_notification\n"));
}
In 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
.
In main.c
#include "ble.h"
void application_start( void )
{
bt_le_start_notification();
}
Did I missing something ? Thanks in advance.
Functions in C are global by default. To make them local to the file they are created, we use the keyword static before the function. Static functions can't be called from any other file except the file in which it is created. Using static function, we can reuse the same function name in different files.
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 .
A static method can call only other static methods; it cannot call a non-static method. A static method can be called directly from the class, without having to create an instance of the class.
A function can be declared as static function by placing the static keyword before the function name. Now, if the above code is compiled then an error is obtained i.e “undefined reference to staticFunc()”. This happens as the function staticFunc() is a static function and it is only visible in its object file.
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 i.e non static functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With