15

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.

Martin
  • 2,813
  • 11
  • 43
  • 66
  • 4
    `static void bt_le_start_notification(void);` Why `static` if you mean to call it from another `C file`? – dxiv Feb 16 '16 at 07:03
  • 2
    This is like saying "I locked my valuables inside a bank vault. Now I want all my valuables to be publicly accessible by everyone. How do I give everyone in the world access to my bank vault? The bank refuses to let me do that." Why did you lock them in for in the first place, if you want everyone to access them? – Lundin Feb 16 '16 at 10:13

6 Answers6

25
 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.

Anbu.Sankar
  • 1,326
  • 8
  • 15
  • why i can still access to static function after include it with gcc (Xcode) ??! and i can't if i use inline. What's wrong with gcc of XCode? – TomSawyer Apr 26 '20 at 20:55
15

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.

1.c

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

2.c

static void call1(){
printf("a \n");
}
(*func)() = &call1;
sourav punoriyar
  • 830
  • 8
  • 18
  • 8
    Sure it does. You sometimes need to break encapsulation when writing automated unittests. This is completely legitimate. – user7610 Dec 11 '16 at 09:43
8

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. 1. Create a function pointer globally in ble.c and define it.

(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

 #include "ble.h"
extern fn_ptr;

void application_start( void )
{
  fn_ptr();
}

Hope it will be useful.

  • Unnecessary complexity. If a function is not meant to be internal to a translation unit, it should not be defined as `static`. – Michael Foukarakis Feb 16 '16 at 08:10
  • @MichaelFoukarakis there are situations where clients proprietary code should be retained and improvement should be shown on using there code as it is, so that is where you will need to use a static function in another source file, so that is where vivek trick works – VRU Sep 28 '16 at 12:30
1

The static function scope is file(i.e. translation unit) where it is defined.

Umamahesh P
  • 1,224
  • 10
  • 14
1

The keyword static is often used to encapsulate the function in the source file where it is defined. So it is not meant to call a static function from outside, an other c-file. An extern article[^] is explaining that topic pretty good I think.

Quote:

Static functions are much like private methods in Java or 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. 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.

Frodo
  • 749
  • 11
  • 23
0

You get this message because you have declared the function to be static. So the implementation is only visible inside your .c file. Try removing the static from both your .h and .c, this should allow your function to be seen.

Teodorico Levoff
  • 1,641
  • 2
  • 26
  • 44