C 警告:函数的隐式声明

C 警告:函数的隐式声明,c,compiler-warnings,C,Compiler Warnings,我的编译器(GCC)向我发出警告: 警告:函数的隐式声明 请帮助我理解它为什么会出现。您正在使用一个编译器尚未看到声明(“原型”)的函数 例如: int main() { fun(2, "21"); /* The compiler has not seen the declaration. */ return 0; } int fun(int x, char *p) { /* ... */ } 您需要在main之前声明函数,如下所示,可以直接声明,也可以

我的编译器(GCC)向我发出警告:

警告:函数的隐式声明


请帮助我理解它为什么会出现。

您正在使用一个编译器尚未看到声明(“原型”)的函数

例如:

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

int fun(int x, char *p)
{
    /* ... */
}
您需要在main之前声明函数,如下所示,可以直接声明,也可以在头中声明:

int fun(int x, char *p);

正确的方法是在头文件中声明函数原型

例子 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);
}
static int some_main(const char *name);

int some_main(const char *name)
{
    // do something
}
带有一个文件(main.c)的备选方案


如果您定义了正确的头并且正在使用非
GlibC
库(例如
gcc
),那么当遇到GNU扩展(例如
malloc\u trim
)时,也会抛出
错误:隐式函数声明

解决办法是:


当您得到
错误:函数的隐式声明时
还应该列出有问题的函数。此错误通常是由于忘记或缺少头文件而发生的,因此在shell提示下,您可以键入
man 2 functionname
并查看顶部的
Synosis
部分,因为此部分将列出需要包含的任何头文件。或者试试这是联机手册页,它们是超链接的,易于搜索。 函数通常在头文件中定义,包括任何必需的头文件通常就是答案。正如cnicutar所说


您正在使用一个编译器尚未看到的函数 声明(“原型”)尚未发布


我认为这个问题没有得到100%的回答。我正在搜索缺少typeof()的问题,这是编译时指令

以下链接将说明情况:

从结论开始,尝试使用
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。还有
gcc-Dtypeof=\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。
e、 g.假设这是main.c,引用的函数在“SSD1306_LCD.h”中

#包括“SSD1306_LCD.h”
#包括“system.h”#包括
#包括
#包括
#包括
#包括
#包括//http://microchip.wikidot.com/faq:74
#包括
#包括
#包括“GenericTypeDefs.h”//此具有“BYTE”类型定义
上述操作不会生成“隐式函数声明”错误,但以下操作将-

#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"    
#包括“system.h”
#包括
#包括
#包括
#包括
#包括
#包括//http://microchip.wikidot.com/faq:74
#包括
#包括
#包括“GenericTypeDefs.h”//此具有“BYTE”类型定义
#包括“SSD1306_LCD.h”
完全相同的包含列表,只是顺序不同


对我来说是的

在使用main函数之前,需要声明所需的函数:

#include <stdio.h>
int yourfunc(void);

int main(void) {

   yourfunc();
 }
#包括
int yourfunc(void);
内部主(空){
yourfunc();
}

不要忘记,如果在函数中调用的任何函数及其原型必须位于代码中函数的上方,否则编译器可能无法在尝试编译函数之前找到它们。这将产生有问题的错误。

如果您已经给原型检查,确认它不仅仅是一个打字错误,那么这将作为一个补充。此外,如果它来自外部库,请检查您是否已包含它。收到此警告后,我无法运行代码。所以它的行为就像一个错误。@Flimm、C99和C89/C90的设置不同this@Flimm@ZachSaw这样做是正确的。否则你就不会重复自己三次了。A“为什么它不给出一个错误版本”:如果你忘记包含头文件,也会发生这种情况。例如,如果您试图在不包含string.h的情况下使用strlen(),则会出现此错误。这是否添加了其他答案尚未提供的内容?
#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 <stdio.h>
int yourfunc(void);

int main(void) {

   yourfunc();
 }