C语言 在 C 中定义但未使用的函数警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2845748/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:27:29  来源:igfitidea点击:

Function defined but not used warning in C

cwarnings

提问by thetna

I have a number of C source files(both .c and .h files). header files contains a number of functions. Out of those functions, only partially are used in a source .C file.Suppose a.h,b.h are header files and a.c and b.c are .c files. a.h is included in a.c. But only a number of functions those are in a. h are used and rest are not used. After compilation I find following warnings:

我有许多 C 源文件(.c 和 .h 文件)。头文件包含许多函数。在这些函数中,仅部分用于源 .C 文件。假设 ah,bh 是头文件,而 ac 和 bc 是 .c 文件。ah 包含在 ac 中,但只有一些函数包含在 a 中。h 被使用,其余未被使用。编译后,我发现以下警告:

 function XXXX defined but not used.

But those XXXX functions which are not used in a.c are used in b.c. So, i can't completely remove those functions too. So , i decided to make a separate file containing only those XXXX functions and included it wherever it is used.Doing this is creating multiple number of header files. Can anybody please suggest me to some effective way to solve this problem.

但是那些不在 ac 中使用的 XXXX 函数在 bc 中使用,所以,我也不能完全删除这些函数。所以,我决定制作一个单独的文件,只包含那些 XXXX 函数,并将它包含在任何使用它的地方。这样做是创建多个头文件。任何人都可以建议我一些有效的方法来解决这个问题。

回答by AnT

"Function defined but not used" warning is only issued for functions with internal linkage, i.e. functions that are declared as static. These functions are only accessible in one translation unit, so the compiler always knows whether they are used (in the program) or not. If you don't reference these functions in their translation unit, these functions are known to be unused, and the warning is generated.

“函数已定义但未使用”警告仅针对具有内部链接的函数发出,即声明为 的函数static。这些函数只能在一个翻译单元中访问,所以编译器总是知道它们是否被使用(在程序中)。如果您不在其翻译单元中引用这些函数,则已知这些函数未被使用,并生成警告。

You are saying that these functions "are not used in a.c, but used in b.c". This is not true. When you declare (and define) a function as staticin the header file, each translation unit that includes that header file gets its own internal copyof the function. Even though these functions look absolutely the same, they are still separate, completely independent functions. The fact that they have the same name and consist of the same code means nothing to the compiler. So, in b.cyou got a completely independent copy of the function, which is used (as you say), but the completely independent copy in a.cis still not used.

您是说这些函数“不在 ac 中使用,而是在 bc 中使用”。这不是真的。当您static在头文件中声明(并定义)一个函数时,包含该头文件的每个翻译单元都会获得其自己的函数内部副本。尽管这些功能看起来完全相同,但它们仍然是独立的、完全独立的功能。它们具有相同的名称并由相同的代码组成这一事实对编译器毫无意义。所以,在b.c你得到了一个完全独立的函数副本,它被使用了(如你所说),但a.c仍然没有使用完全独立的副本。

The question in this case is whyyou are doing this. Why on Earth are you defining staticfunctions in the header file? If you really need to do this (i.e. if you really want to spawn a separate internal "clone" of this function in each translation unit), you can work around the warning by using some compiler-specific means. Like in GCC, for example, you can declare the function with __attribute__((unused))an the warning for this function will no longer be issued.

在这种情况下的问题是你为什么要这样做。你到底为什么要在头文件中定义静态函数?如果您确实需要这样做(即,如果您真的想在每个翻译单元中生成此函数的单独内部“克隆”),您可以使用一些特定于编译器的方法来解决警告。就像在 GCC 中一样,例如,您可以声明该函数,__attribute__((unused))并且不再发出此函数的警告。

But normally one wouldn't need to define functions in the header file. Normally one'd use a function with externallinkage (i.e. no static), define it in one of the .c files and put the declaration (prototype) in the header file. The compiler will not issue any warnings in this case, even if the function is declared but not used in some translation unit.

但通常不需要在头文件中定义函数。通常人们会使用具有外部链接(即 no static)的函数,在 .c 文件之一中定义它并将声明(原型)放在头文件中。在这种情况下,编译器不会发出任何警告,即使该函数已声明但未在某些翻译单元中使用。

回答by Jon Chesterfield

As an alternative to "don't do that", consider the following - a set of functions that will trigger up to three unused function warnings.

作为“不要这样做”的替代方案,请考虑以下内容 - 一组函数将触发最多三个未使用的函数警告。

static int get_version_number(void) { return 42; }
static double hidden_global_variable(void) { return 3.14; }
static int potential_alternative_to_macro(int x) { return 4 * x; } 

Write another function, probably based on the name of the header file,

再写一个函数,大概是根据头文件的名字,

static void wno_unused_myheadername(void)
{
  /* don't need to actually call the functions to avoid the warnings */
  (void)&get_version_number;
  (void)&hidden_global_variable;
  (void)&potential_alternative_to_macro;
  return;
 }

We're now down to one unused function warning. If you add a call to wno_unused_myheadername() in any of the extern functions declared in the file that includes the header, the whole set of unused function warnings will disappear. Since they're now all used.

我们现在只剩下一个未使用的函数警告。如果在包含头文件的文件中声明的任何外部函数中添加对 wno_unused_myheadername() 的调用,则整套未使用的函数警告将消失。因为现在都用上了。

The compiler will strip out anywhere from all to none of the unused functions, including the wno_unused_myheadername, since it can see all of the definitions and can probably determine that the single call to the wno_unused function doesn't actually do anything.

编译器将删除所有未使用的函数,包括 wno_unused_myheadername,因为它可以看到所有定义,并且可能可以确定对 wno_unused 函数的单个调用实际上没有做任何事情。

I've checked that the above removes the warnings as expected under clang and gcc, your milage may vary with other compilers. I haven't looked at the asm output to investigate when the nearly-unused functions are stripped.

我已经检查过上述内容在 clang 和 gcc 下按预期删除了警告,您的里程可能因其他编译器而异。我没有查看 asm 输出来调查几乎未使用的函数何时被剥离。

As for why - a good reason would be using a lot of small functions which are well suited to inlining in C89, which doesn't have the inline keyword, without requiring link time optimisation from your compiler.

至于为什么 - 一个很好的理由是使用许多非常适合在 C89 中内联的小函数,它没有 inline 关键字,不需要编译器的链接时间优化。

回答by thomasrutter

If you just want to hide the warning, use:

如果您只想隐藏警告,请使用:

-Wno-unused-function

However, you should probably follow the advice in caf's answerinstead. It sounds like you may have defined a function when you only meant to add its declaration.

但是,您可能应该遵循caf 答案中的建议。听起来您可能已经定义了一个函数,而您只是想添加它的声明。

回答by coombe

Another possibility is to define these functions as inlineinstead of static. In this case, they are required to be defined in the header so that the definition is visible everywhere they are called.

另一种可能性是将这些函数定义为inline而不是static。在这种情况下,需要在标题中定义它们,以便在调用它们的任何地方都可以看到定义。

The compiler will inline the code in each place that the function is used, so be careful that this is what you really want. Here's an answerwith a nice discussion of these tradeoffs.

编译器将在使用该函数的每个地方内联代码,因此请注意这是您真正想要的。这是一个答案,其中对这些权衡进行了很好的讨论。

回答by caf

It sounds like your problem is that you're definingfunctions in .hfiles. Don't do that. Instead, just put your declarationsin the .hfile, and have a matching .cfile that contains the function definitions:

听起来你的问题是你在文件中定义函数.h。不要那样做。相反,只需将您的声明放在.h文件中,并有一个.c包含函数定义的匹配文件:

common.h:

常见的.h:

#ifndef _COMMON_H
#define _COMMON_H

int foo(int a, int b);

int bar(double x, double y, double z);

#endif /* _COMMON_H */

common.c:

通用.c:

#include "common.h"

int foo(int a, int b)
{
    /* code */
}

int bar(double x, double y, double z)
{
    /* code */
}

Then your a.cand b.cshould #include "common.h", and you'll need to arrange to have common.ccompiled into the complete program.

然后你的a.cb.cshould #include "common.h",你需要安排已经common.c编译成完整的程序。

回答by tlr

No one seems to be mentioning that static function definitions in .h files make very good typed macros in C. I think that is a very valid way to use this type of construct but then it needs to be made warning free of course.

似乎没有人提到 .h 文件中的静态函数定义在 C 中生成了非常好的类型化宏。我认为这是使用这种类型的构造的一种非常有效的方法,但当然需要使其免警告。

The common.c/common.h solution suggestion by caf will work around such a warning but instead generate a function call for each instance of the function which you may not want.

caf 的 common.c/common.h 解决方案建议将解决此类警告,而是为您可能不想要的每个函数实例生成函数调用。