警告:隐式声明函数

Ang*_*gus 180 c compiler-warnings

我的编译器(GCC)给了我警告:

警告:隐式声明函数

请帮我理解为什么会这样.

cni*_*tar 209

您正在使用编译器尚未看到声明(" 原型 ")的函数.

例如:

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

int fun(int x, char *p)
{
    /* ... */
}
Run Code Online (Sandbox Code Playgroud)

你需要在main之前声明你的函数,比如直接或在标题中:

int fun(int x, char *p);
Run Code Online (Sandbox Code Playgroud)

  • 作为补充,如果您已经给出原型检查它不仅仅是一个错字.此外,如果它来自外部库检查您是否已包含它. (7认同)
  • @Flimm http://stackoverflow.com/questions/434763/are-prototypes-required-for-all-functions-in-c89-c90-or-c99 (3认同)

小智 18

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

main.h

#ifndef MAIN_H
#define MAIN_H

int some_main(const char *name);

#endif
Run Code Online (Sandbox Code Playgroud)

main.c中

#include "main.h"

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

int some_main(const char *name)
{
    printf("%s", name);
}
Run Code Online (Sandbox Code Playgroud)

替代一个文件(main.c)

static int some_main(const char *name);

int some_main(const char *name)
{
    // do something
}
Run Code Online (Sandbox Code Playgroud)


小智 7

在main.c中执行#includes时,将#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
Run Code Online (Sandbox Code Playgroud)

以上内容不会产生"隐含的功能声明"错误,但在下面将 -

#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"    
Run Code Online (Sandbox Code Playgroud)

完全相同的#include列表,只是不同的顺序.

嗯,它对我有用.