Loose-Info.com
Last Update 2021/04/22
TOP - 各種テスト - gcc - 警告関連のオプション - -Wextra

-Wextra
-Wallで有効となっていない追加の警告を有効化

テスト概要

その1
-Wextraオプションを使用してコンパイルを実行した場合の警告出力

その2
-Wextraと-Wallを同時に使用した場合の警告出力

実行環境

GCC-8.2.0
GNU C Library 2.28
GNU Binutils 2.31.1


コード例・出力内容中の表記

・実行例中の太字表記部分は、コマンドなどの入力された文字列を示します。
・「」や「...」の着色省略表記は、 実際のソースコードや出力内容などを省略加工した部分を示します。

使用ファイル


sample.c
#include <stdio.h> double sampFunc1(double d1, double d2) { return d1 + d2; } const void sampFunc2(void) { /* 関数の戻り値の型に型修飾子 [-Wignored-qualifiers] */ } int main(void) { double (*sampPtr1)(double, double); sampPtr1 = sampFunc1; int n = 1; int *pn = &n; /* ポインタを0(整数)と演算子「>」で比較 [-Wextra] */ if (pn > 0) { printf("-Wextra\n"); } /* 関数ポインタの互換性の無い関数ポインタへのキャスト */ /* 変換指定子「%f」と互換性の無い型へキャストされた関数ポインタ */ printf("func1 = %f\n", ((int (*)(int, int))sampPtr1)(2.2, 3.3)); return 0; }

その1

-Wextraオプションを使用してコンパイルを実行
$ gcc -Wextra sample.c sample.c:8:1: warning: type qualifiers ignored on function return type [-Wignored-qualifiers] const void sampFunc2(void) ^~~~~ sample.c: In function ‘main’: sample.c:23:9: warning: ordered comparison of pointer with integer zero [-Wextra] if (pn > 0) ^ sample.c:30:26: warning: cast between incompatible function types from ‘double (*)(double, double)’ to ‘int (*)(int, int)’ [-Wcast-function-type] printf("func1 = %f\n", ((int (*)(int, int))sampPtr1)(2.2, 3.3)); ^ $ -Wextraオプションが3つの警告を出力

その2

-Wextraと-Wallを同時に使用して実行
着色部は-Wallによる出力部
$ gcc -Wextra -Wall sample.c sample.c:8:1: warning: type qualifiers ignored on function return type [-Wignored-qualifiers] const void sampFunc2(void) ^~~~~ sample.c: In function ‘main’: sample.c:23:9: warning: ordered comparison of pointer with integer zero [-Wextra] if (pn > 0) ^ sample.c:30:26: warning: cast between incompatible function types from ‘double (*)(double, double)’ to ‘int (*)(int, int)’ [-Wcast-function-type] printf("func1 = %f\n", ((int (*)(int, int))sampPtr1)(2.2, 3.3)); ^ sample.c:30:19: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘int’ [-Wformat=] printf("func1 = %f\n", ((int (*)(int, int))sampPtr1)(2.2, 3.3)); ~^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ %d $