missing function header (old-style formal list?) 含義與修正missing function header (old-style formal list?) 直譯:「缺少函數標頭(舊式形式參數表?)」。
int),現代編譯器 (C99+) 拒絕隱式 int。foo(a, b) int a; char b; { ... } 而非標準 foo(int a, char b) { ... }。int func(); (舊式無參數資訊) 定義 int func(int x) { ... }。{ 開頭。| 錯誤代碼 | 問題 | 修正代碼 |
|---|---|---|
main() { ... } | 隱式 int 返回型別 | int main(void) { ... return 0; } |
func(a, b) int a; float b; { ... } | K&R 舊式參數宣告 | void func(int a, float b) { ... } |
int add(); 後定義 int add(int x, int y) { return x+y; } | 宣告未帶參數型別 | int add(int x, int y); (原型宣告) |
void foo() int x = 1; { ... } | 大括號前有語句 | void foo(void) { int x = 1; ... } |
// 1. 原型宣告 (頭檔/檔案頂部)
int add(int a, int b);
void print_msg(const char *msg);
// 2. 定義
int add(int a, int b) {
return a + b;
}
void print_msg(const char *msg) {
if (msg) puts(msg);
}
// 3. main 規範
int main(void) {
printf("%d\n", add(3, 4));
print_msg("Hello");
return 0;
}
gcc -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition
cl /std:c11 /Wall /WX /W4
-Wmissing-prototypes:警告未前置宣告的非靜態函數。-Wstrict-prototypes:警告 func() 空參數列表(應寫 func(void))。-Wold-style-definition:警告 K&R 風格定義。