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
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 thestatic
keyword both in thea.h
anda.c
so that other .c files can see the function. You should specify a return value, e.g.void function1();
because it implicitly isint
if you didn't specify one.Declare the function normally in the header file
a.h
Define the function in a single code file with no
static
a.c
and call the function from other files, after including the header
b.c
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).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 theunused
function attribute to suppress this warning: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. Lonestatic
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
andinline
keywords from its definition and move the definition from the header into that file.Remove the
static
andinline
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...