【问题标题】:warning: implicit declaration of function - order of includes matter?警告:函数的隐式声明 - 包含问题的顺序?
【发布时间】:2016-09-13 01:11:54
【问题描述】:

我有以下来自 main.c 的示例代码 sn-p 代码,它调用 3 个函数,带有 3 个标头 - 这给了我一个未知原因的警告:

#include "header1.h"
#include "header2.h"
#include "header3.h"

int main()
{
  function1(); // this is from header1
  function2(); // this is from header2
  function3(); // this is from header 3
} 

基本上,使用 gcc 后,函数 2 和 3 会产生警告。但是,在将代码重新排列为如下内容后:

#include "header3.h"
#include "header1.h"
#include "header2.h"
   

int main()
{
  function1(); // this is from header1
  function2(); // this is from header2
  function3(); // this is from header 3
} 

然后它会警告我函数 1 和 2 是隐式的。我在这里做错了什么?

【问题讨论】:

  • 请提供minimal reproducible example。我们需要查看头文件的确切内容。你可能有一个/所有的错误(使用相同的包含保护是我的猜测)。
  • 如果标头未正确包含依赖标头或使用晦涩的宏/条件编译,则会发生此类错误。见How to Ask,提供minimal reproducible example
  • @kaylum 这里是其中一个头文件,其余的跟风#ifndef __REZON_FUNCTIONS__ #define __REZON_FUNCTIONS__ void function1(); #endif
  • 不,请通过单击“编辑”链接在问题中发布。 cmets 中的代码不可读。请张贴所有的标题。 “休息跟风”可以意味着任何事情——你认为你所做的可能与我认为你所做的不同。而那些 exact 标题内容是这个问题的关键。请输入准确的 C 代码,并清楚标头中没有名为 function1 等的函数。
  • @bruceg 这行得通,非常感谢!

标签: c warnings declaration implicit


【解决方案1】:

您的包含文件 1 应如下所示:

#ifndef __REZON_FUNCTIONS1__ 
#define __REZON_FUNCTIONS1__ 

#endif

其他两个文件应该是相似的,相应地更改了宏名称

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多