gcc warning: function used but not defined

2020-05-25 07:03发布

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

3条回答
Emotional °昔
2楼-- · 2020-05-25 07:34

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.

查看更多
时光不老,我们不散
3楼-- · 2020-05-25 07:38

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

查看更多
Juvenile、少年°
4楼-- · 2020-05-25 07:44

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

查看更多
登录 后发表回答