21

I am getting the warning: function used but not defined. I have static __inline__ in header file say a.h. The header file is included in a.c. I would like put all those inline function which are in header files into the .c files. Following code gives the idea of my problem.

Orginal code:

a.h:

static __inline__ function1(){
    function definition;  
}

I changed:
a.h:

static function1();

a.c:

#include "a.h"

static function1(){
   function definition;
}

On doing above I got the warning:

   warning: function function1 is used but not defined. 

Could you please let me know why i am getting such warning? I would like to transfer all the __inline__ function into the .c so that I won't get the warning:

  warning: function1 is could not be inlined, code size may grow.

Thanks in advance

sarnold
  • 102,305
  • 22
  • 181
  • 238
thetna
  • 6,903
  • 26
  • 79
  • 113

3 Answers3

61

You've declared the function to be static. This means that it is only visible within the current compilation unit. In other words: the implementation is only visible inside the a.c file. You need to remove the static keyword both in the a.h and a.c so that other .c files can see the function. You should specify a return value, e.g. void function1(); because it implicitly is int if you didn't specify one.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
9

Functions declared static within a .c file are only visible/usable within that file only. If they are not used in it, then they are effectively dead code and the compiler warns you about this fact. In GCC you can use the unused function attribute to suppress this warning:

static int __attribute__((unused)) function1() {
...
}

EDIT:

In general you should usually follow the following guidelines regarding inline functions:

  • If they are used in multiple C files, declare them static and have their definition in an included header file. That allows all .c files that include that header to have their own private definition of the function, which allows the compiler to inline it. Lone static function prototypes make little to no sense in a header file that will be used by multiple source files, since their actual definitions will be missing.

  • If they are not intended to be reused, have their definition (and, if necessary, their prototype) in the .c file where they are supposed to be used.

If GCC complains about being unable to inline a function, due to the function size:

  • Ask yourself if you really need that function to be inlined - from my experience, the compiler usually knows best.

  • If you really, really want that function inlined, the always_inline function attribute may be of use. You may also have to provide a non-default -finline-limit=n option to GCC to increase the allowed size for inline functions.

See also this for additional information on inline functions and some possible pitfalls regarding their use.

EDIT 2:

If you have a static inline function defined in a shared header file and want to turn it into a normal, for lack of a better word, function you should:

  • Select a .c file where the presence of that function make sense (i.e. put it with other related functions).

  • Remove the static and inline keywords from its definition and move the definition from the header into that file.

  • Remove the static and inline keywords from its prototype and put it into the header file.

Congratulations, you now have a normal publicly-available function.

Disclaimer: you just made a function that was private to a number of files, public to all of your program. If there is another public symbol - variable or function - with the same name, you may get errors while linking or even strange behaviour at runtime. You've been warned...

thkala
  • 84,049
  • 23
  • 157
  • 201
  • Thanks for the descriptive answer.Actually i don't want to **inline** the function. So keeping the functionality same, I would like to remove the **inline**. Since i am defining the particular inlined function in the C file, does the keyword **static** in front of the function creating the problem?> – thetna Apr 02 '11 at 23:34
  • `static` in a function definition makes that function *private* to that `.c` file only. If that function is not actually used in that `.c` file it's dead code, which is why the compiler complains. – thkala Apr 02 '11 at 23:37
4

Declare the function normally in the header file

a.h

#ifndef A_H_INCLUDED
#define A_H_INCLUDED

void function1(void);

#endif

Define the function in a single code file with no static

a.c

#include "a.h"

void function1(void) {
  /* function definition */
}

and call the function from other files, after including the header

b.c

#include "a.h"

void quux(void) {
  function1(); /* call regular function */
}

The way you had before (static and implementation in header file) worked because each code file that included that header got its own version of the function; different to every other function of the same name in every other file (but doing exactly the same).

pmg
  • 106,608
  • 13
  • 126
  • 198
  • do you mean i should not keep the function prototype in header file itself? The particular header file is included in the different C files too.Or is it the keyword **static** is making differnece? – thetna Apr 02 '11 at 23:10
  • 1
    The header file is to help other files (translation units) know the correct way to call the function. If the function is only available to one single .c file (the `static`), there is no point adding the prototype to the .h file. – pmg Apr 02 '11 at 23:13
  • I just want to remove the keyword **inline** keeping the functionality of the code same as before. What do you suggest in this case? Those inlined functions were called in other C files too.Isn't is require to define it in header file in this case? – thetna Apr 02 '11 at 23:22
  • 1
    Then you can't declare the function `static`. Keep the prototype in one single header file (`a.h`), the definition in one single code file (`a.c`) and `#include "a.h"` in the other code files. – pmg Apr 02 '11 at 23:29