2011-12-09 91 views
140

我的编译器(GCC)是给我的警告:警告:函数隐式声明

警告:函数隐式声明

请帮助我了解为什么它的到来。

+1

“为什么它不提供错误版本”:http://stackoverflow.com/questions/434763/are-prototypes-required-for-all-functions-in-c89-c90-or-c99 –

回答

163

您正在使用编译器未见过声明的函数(“原型”)。

例如:

int main() 
{ 
    fun(2, "21"); /* The compiler has not seen the declaration. */  
    return 0; 
} 

int fun(int x, char *p) 
{ 
    /* ... */ 
} 

您需要直接或在头前主来声明功能,这样,:

int fun(int x, char *p); 
+7

作为如果你已经给出原型检查它不只是一个错字,那么还有一个补充。另外,如果它来自外部库,则检查是否包含它。 – smitec

+40

这是一个警告而不是错误? – Flimm

+1

我收到此警告后无法运行代码。所以它表现得像一个错误。 – Mien

13

正确的方法是声明函数原型在头。

main.h

#ifndef MAIN_H 
#define MAIN_H 

int some_main(const char *name); 

#endif 

的main.c

#include "main.h" 

int main() 
{ 
    some_main("Hello, World\n"); 
} 

int some_main(const char *name) 
{ 
    printf("%s", name); 
} 

替代与一个文件(main.c中)

static int some_main(const char *name); 

int some_main(const char *name) 
{ 
    // do something 
} 
2

如果定义&正确的头使用非GlibC库(如Musl Cgcc也将抛出error: implicit declaration of function遇到GNU扩展,如malloc_trim时。

的解决方案是wrap the extension & the header

#if defined (__GLIBC__) 
    malloc_trim(0); 
#endif 
2

当你得到error: implicit declaration of function也应该列出有问题的功能。通常这个错误是由于一个被遗忘或丢失的头文件而发生的,所以在shell提示符下你可以输入man 2 functionname并查看顶部的SYNOPSIS部分,因为本节将列出需要包含的所有头文件。或者试试http://linux.die.net/man/这是他们超链接并且易于搜索的在线手册页。 函数通常在头文件中定义,包括任何需要的头文件通常都是答案。就像cnicutar说,

您正在使用该编译器还没有见过一个 声明(“原型”),但功能。

0

如果忘记包含头文件,也会发生这种情况。例如,如果您尝试在不包含string.h的情况下使用strlen(),您将得到此错误

5

当您在main.c中执行#include时,将#include引用放置到包含引用函数的文件中包含列表的顶部。 例如之所以这样说,是的main.c和你引用的功能是在“SSD1306_LCD.h”

#include "SSD1306_LCD.h"  
#include "system.h"  #include <stdio.h> 
#include <stdlib.h> 
#include <xc.h> 
#include <string.h> 
#include <math.h> 
#include <libpic30.h>  // http://microchip.wikidot.com/faq:74 
#include <stdint.h> 
#include <stdbool.h> 
#include "GenericTypeDefs.h" // This has the 'BYTE' type definition 

以上不会产生错误“的功能隐式声明”,但下面将 -

#include "system.h"   
#include <stdio.h> 
#include <stdlib.h> 
#include <xc.h> 
#include <string.h> 
#include <math.h> 
#include <libpic30.h>  // http://microchip.wikidot.com/faq:74 
#include <stdint.h> 
#include <stdbool.h> 
#include "GenericTypeDefs.h"  // This has the 'BYTE' type definition 
#include "SSD1306_LCD.h"  

没错相同的#include列表,只是不同的顺序。

嗯,它为我做了。