我想在代码库中找到未使用的函数 - 包括跨编译单元。我使用 gcc 作为我的编译器。
这是一个例子:
foo.c
(假设适当的 foo.h
):
void foo() {
....
}
void bar() {
....
}
main.c
:
#include <stdio.h>
#include "foo.h"
int main(void) {
bar();
return 0;
}
在这个例子中,我想收到有关 foo()
未被使用的警告。
有 -Wunused-function
gcc 选项:
-Wunused-function
Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall.
但它仅适用于静态函数 - 它不会在上面的示例中产生警告。
我也会接受可以为我执行此操作的工具/脚本/其他编译器的建议 - 尽管如果可能的话我更愿意坚持使用 gcc
。
最佳答案
Caolan Mc Namara ,一名 LibreOffice 开发人员,制作了一个小工具来检测 LibreOffice 源代码中的此类内容。他们有大约数千个 LibreOffice 中未使用的函数和方法。他的工具是去除它们的关键因素。
它叫做 callcatcher .可以
collect functions/methods defined and subtract called/referenced
它直接作用于汇编器输出,因此它只适用于 x86 和 x86_64 架构。它可以产生类似 this 的输出.您可以将其与对 gcc 的传统编译和链接调用集成。
Caolan同意它应该成为一个gcc插件。
关于c - 有没有办法就未使用的功能发出警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9091397/