C语言 gcc 警告:函数已使用但未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5526461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gcc warning: function used but not defined
提问by thetna
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 .cfiles. Following code gives the idea of my problem.
我收到警告:function used but not defined。我static
__inline__在头文件中说a.h。头文件包含在a.c. 我想将头文件中的所有内联函数放入.c文件中。以下代码给出了我的问题的想法。
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 .cso that I won't get the warning:
你能告诉我为什么我会收到这样的警告吗?我想将所有__inline__函数转移到 中,.c这样我就不会收到警告:
warning: function1 is could not be inlined, code size may grow.
Thanks in advance
提前致谢
回答by DarkDust
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.cfile. You need to remove the statickeyword both in the a.hand a.cso that other .c files can see the function. You should specify a return value, e.g. void function1();because it implicitly is intif you didn't specify one.
您已将该函数声明为静态函数。这意味着它仅在当前编译单元中可见。换句话说:实现只在a.c文件内可见。您需要删除and中的static关键字a.h,a.c以便其他 .c 文件可以看到该函数。您应该指定一个返回值,例如,void function1();因为int如果您没有指定一个,它就隐含地存在。
回答by thkala
Functions declared staticwithin a .cfile 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 unusedfunction attributeto suppress this warning:
static在.c文件中声明的函数仅在该文件中可见/可用。如果它们没有在其中使用,那么它们实际上是死代码,编译器会警告您这一事实。在 GCC 中,您可以使用unusedfunction 属性来抑制此警告:
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
staticand have their definition in an included header file. That allows all.cfiles that include that header to have their own privatedefinition of the function, which allows the compiler to inline it. Lonestaticfunction prototypesmake 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
.cfile where they are supposed to be used.
如果它们在多个 C 文件中使用,则声明它们
static并在包含的头文件中定义它们。这允许.c包含该头文件的所有文件都有自己的函数的私有定义,这允许编译器内联它。龙static函数原型做一点,它们将由多个源文件使用的头文件没有意义的,因为他们的实际定义将丢失。如果不打算重用它们,请将它们的定义(以及,如有必要,它们的原型)放在
.c应该使用它们的文件中。
If GCC complains about being unable to inline a function, due to the function size:
如果 GCC 抱怨由于函数大小而无法内联函数:
Ask yourself if you reallyneed that function to be inlined - from my experience, the compiler usually knows best.
If you really, really want that function inlined, the
always_inlinefunction attributemay be of use. You may also have to provide a non-default-finline-limit=noption to GCC to increase the allowed size for inline functions.
问问自己是否真的需要内联该函数 - 根据我的经验,编译器通常最了解。
如果您真的非常希望内联该
always_inline函数,那么function 属性可能会有用。您可能还必须为-finline-limit=nGCC提供一个非默认选项来增加内联函数的允许大小。
See also thisfor additional information on inline functions and some possible pitfalls regarding their use.
另请参见本上的有关使用内联函数和一些可能出现的问题的其他信息。
EDIT 2:
编辑2:
If you have a static inlinefunction defined in a shared header file and want to turn it into a normal, for lack of a better word, function you should:
如果你static inline在共享头文件中定义了一个函数,并且想把它变成一个普通的函数,因为没有更好的词,函数你应该:
Select a
.cfile where the presence of that function make sense (i.e. put it with other related functions).Remove the
staticandinlinekeywords from its definition and movethe definition from the header into that file.Remove the
staticandinlinekeywords from its prototypeand put it into the header file.
选择一个
.c文件,其中该函数的存在有意义(即,将其与其他相关函数放在一起)。从其定义中删除
static和inline关键字,并将定义从标题移动到该文件中。从其原型中删除
static和inline关键字并将其放入头文件中。
Congratulations, you now have a normal publicly-available function.
恭喜,您现在拥有了一个普通的公开可用的函数。
Disclaimer: you just made a function that was private to a number of files, publicto 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...
免责声明:您刚刚创建了一个对许多文件私有的函数,对您的所有程序都是公开的。如果有另一个公共符号 - 变量或函数 - 具有相同的名称,您可能会在链接时出错,甚至在运行时出现奇怪的行为。你已经被警告...
回答by pmg
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
在单个代码文件中定义函数,没有 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 (staticand 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).
您之前的方式(static以及头文件中的实现)有效,因为包含该头文件的每个代码文件都有自己的函数版本;与每个其他文件中的所有其他同名函数不同(但完全相同)。

