Last Update 2021/09/21
-Wunused-function
static関数が宣言があっても定義されていない場合、inline指定の無いstatic関数が使用されていない場合に警告を出力
テスト概要
オプション無し、-Wunused-function、-Wallの各オプションを使用した際の警告出力例
実行環境
GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1
GNU C Library 2.28
GNU Binutils 2.31.1
コード例・出力内容中の表記
・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「︙」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。
・「︙」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。
使用ファイル
sample.c
#include <stdio.h>
/* 宣言のみで関数定義の無いstatic関数 */
static int sampFunc1(void);
/* 使用されることのない、inline指定されていないstatic関数 */
static int sampFunc2(void)
{
return 1;
}
/* 使用されることのない、inline指定されたstatic関数 */
static inline int sampFunc3(void)
{
return 2;
}
int main(void)
{
printf("test\n");
return 0;
}
動作テスト
オプション無しでコンパイルを実行
$ gcc sample.c エラー・警告無し
$
-Wunused-functionオプションを指定してコンパイルを実行
$ gcc -Wunused-function sample.c
sample.c:4:12: warning: ‘sampFunc1’ declared ‘static’ but never defined [-Wunused-function]
static int sampFunc1(void); static関数sampFunc1が定義されていないことに関する警告
^~~~~~~~~
sample.c:7:12: warning: ‘sampFunc2’ defined but not used [-Wunused-function]
static int sampFunc2(void) 関数sampFunc2が使用されていないことに関する警告
^~~~~~~~~
$ 関数sampFunc3はinline指定のため警告無し
-Wallオプションを指定してコンパイルを実行
$ gcc -Wall sample.c
sample.c:4:12: warning: ‘sampFunc1’ declared ‘static’ but never defined [-Wunused-function]
static int sampFunc1(void);
^~~~~~~~~
sample.c:7:12: warning: ‘sampFunc2’ defined but not used [-Wunused-function]
static int sampFunc2(void)
^~~~~~~~~
$ -Wunused-functionオプションは-Wallでも有効となる