Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call the static function from another c file?

Tags:

c

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.

like image 662
Martin Avatar asked Feb 16 '16 06:02

Martin


People also ask

Can I call a static function from another file in C?

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.

Can static functions be called from another file?

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 .

Can a static function call another static function?

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.

How can a static function be called in the main function?

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.


1 Answers

 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.

like image 77
Anbu.Sankar Avatar answered Sep 23 '22 09:09

Anbu.Sankar