警告:隐式声明函数

时间:2011-12-09 03:49:02

标签: c compiler-warnings

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

  

警告:隐含的功能声明

请帮助我理解为什么会这样。

9 个答案:

答案 0 :(得分:193)

您正在使用编译器尚未看到声明(“ 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);

答案 1 :(得分:16)

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

示例

<强> 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 :(得分:5)

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

嗯,它确实适合我。

答案 3 :(得分:3)

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

  

您正在使用编译器未见过的函数   宣言(&#34;原型&#34;)。

答案 4 :(得分:2)

如果您定义了正确的标题&amp;正在使用非GlibC库(例如Musl Cgcc也会在遇到error: implicit declaration of function等GNU扩展时抛出malloc_trim

解决方案是wrap the extension & the header

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

答案 5 :(得分:1)

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

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

int main(void) {

   yourfunc();
 }

答案 6 :(得分:0)

我认为问题不是100%回答。我正在寻找缺少typeof()的问题,这是编译时指令。

以下链接将阐明情况:

https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Typeof.html

https://gcc.gnu.org/onlinedocs/gcc-5.3.0/gcc/Alternate-Keywords.html#Alternate-Keywords

在结论时尝试使用__typeof__()代替。 gcc ... -Dtypeof=__typeof__ ...也可以提供帮助。

答案 7 :(得分:0)

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

答案 8 :(得分:0)

发生此错误是因为您尝试使用编译器不理解的函数。如果您尝试使用的函数是用 C 语言预定义的,则只需包含与隐式函数关联的头文件。 如果它不是预定义的函数,那么在 main 函数之前声明函数总是一个好习惯。

相关问题