Last Update 2021/09/09
-Wmain
main関数の形式に問題がある場合に警告を出力
テスト概要
オプション無し、-Wmain、-Wall、-Wpedanticの各オプションを使用した際の警告出力例
main関数の引数に問題がある場合の警告出力例
実行環境
GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1
GNU C Library 2.28
GNU Binutils 2.31.1
コード例・出力内容中の表記
・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「︙」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。
・「︙」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。
オプション無し、-Wmain、-Wall、-Wpedanticの各オプションを使用した際の警告出力例
sample1.c
#include <stdio.h>
void main(void)
{
printf("sample\n");
}
オプション無しでコンパイルを実行
$ gcc sample1.c エラー・警告無し
$
-Wmainオプション使用してコンパイルを実行
$ gcc -Wmain sample1.c
sample1.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
void main(void) 戻り値の型が「int」であることに対する警告
^~~~
$
-Wallオプション使用してコンパイルを実行
$ gcc -Wall sample1.c
sample1.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
void main(void)
^~~~
$ -Wallに-Wmainが含まれるため関連する警告を出力
-Wpedanticオプション使用してコンパイルを実行
$ gcc -Wpedantic sample1.c
sample1.c:3:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
void main(void)
^~~~
$ -Wpedanticに-Wmainが含まれるため関連する警告を出力
main関数の引数に問題がある場合の警告出力例
sample2.c
#include <stdio.h>
int main(double d, int n1[], unsigned char uc, int n2)
{
printf("sample\n");
return 0;
}
-Wmainオプション使用してコンパイルを実行
$ gcc -Wmain sample2.c
sample2.c:3:5: warning: first argument of ‘main’ should be ‘int’ [-Wmain]
int main(double d, int n1[], unsigned char uc, int n2)
^~~~
sample2.c:3:5: warning: second argument of ‘main’ should be ‘char **’ [-Wmain]
sample2.c:3:5: warning: third argument of ‘main’ should probably be ‘char **’ [-Wmain]
sample2.c:3:5: warning: ‘main’ takes only zero or two arguments [-Wmain]
$ 各引数の問題点を個別に警告として出力