【问题标题】:warning: implicit declaration of function警告:函数的隐式声明
【发布时间】:2012-01-16 11:16:09
【问题描述】:

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

警告:函数的隐式声明

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

【问题讨论】:

  • A“为什么不给出错误版本”:stackoverflow.com/questions/434763/…
  • 如果您忘记包含头文件,也会发生这种情况。例如,如果您尝试使用 strlen() 而不包含 string.h,您将收到此错误

标签: c compiler-warnings


【解决方案1】:

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

【讨论】:

  • 作为补充,如果你已经给原型检查它不仅仅是一个错字。此外,如果它来自外部库,请检查您是否包含它。
  • 收到此警告后,我无法运行代码。所以它的行为就像一个错误。
  • @Flimm、C99C89/C90 对此有不同的设置
  • @ZachSaw 没错。否则你不会重复三次。
【解决方案2】:

正确的做法是在header中声明函数原型。

示例

ma​​in.h

#ifndef MAIN_H
#define MAIN_H

int some_main(const char *name);

#endif

ma​​in.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
}

【讨论】:

    【解决方案3】:

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

    嗯,对我有用。

    【讨论】:

      【解决方案4】:

      当您获得error: implicit declaration of function 时,它还应该列出有问题的函数。这个错误通常是因为忘记或丢失了头文件,所以在 shell 提示符下你可以输入man 2 functionname 并查看顶部的SYNOPSIS 部分,因为该部分将列出所有需要包含的头文件。或者试试http://linux.die.net/man/ 这是在线手册页,它们带有超链接,易于搜索。 函数通常在头文件中定义,包括任何所需的头文件通常是答案。就像 cnicutar 说的,

      您正在使用编译器没有看到的函数 声明(“原型”)。

      【讨论】:

        【解决方案5】:

        您需要在 ma​​in 函数之前声明所需的函数:

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

        【讨论】:

          【解决方案6】:

          如果您定义了正确的标头并且正在使用非GlibC 库(例如Musl C),则gcc 也会在遇到malloc_trim 等GNU 扩展时抛出error: implicit declaration of function

          解决办法是wrap the extension & the header:

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

          【讨论】:

            【解决方案7】:

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

            【讨论】:

            • 这是否添加了其他答案尚未提供的任何内容?
            【解决方案8】:

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

            【讨论】:

              【解决方案9】:

              我认为这个问题不是 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__ ... 也可以提供帮助。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-12-13
                • 2020-07-28
                相关资源
                最近更新 更多