消息“警告:函数的隐式声明”(implicit declaration of function c)

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

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

警告:函数的隐式声明

为什么会这样?

295

您正在使用编译器尚未看到声明(“prototype”)的函数。

例如:

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);
29

正确的方法是在标题中声明函数原型。

实例

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
}
11

当您在 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

上面不会产生“函数的隐式声明”错误,但会-

#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 列表,只是不同的顺序。

对我来说是的。

4

您需要在main函数之前声明所需的函数:

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

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(375)
将单元格引用到不同工作表中的区域(formula to reference cell a1 from alpha workshe
上一篇
消息“警告:函数的隐式声明”(implicit declaration of function c)
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(7条)