【问题标题】:Implicit declaration of function and conflicting type warning隐式声明函数和冲突类型警告
【发布时间】:2014-08-05 00:31:54
【问题描述】:

我目前正在用 C 语言编写一个程序。一切正常,但是在尝试“make”时我收到了一些警告:

src/command.c: In function ‘cmd_create’:
src/command.c:43:3: warning: implicit declaration of function ‘iterator_init’ [-    Wimplicit-function-declaration]
   iterator_init(&it);
   ^
src/command.c:49:6: warning: implicit declaration of function ‘iterator_destroy’ [-    Wimplicit-function-declaration]
      iterator_destroy(&it);
      ^
src/command.c: At top level:
src/command.c:218:6: warning: conflicting types for ‘iterator_init’ [enabled by default]
 void iterator_init(iterator *it) {
      ^
src/command.c:43:3: note: previous implicit declaration of ‘iterator_init’ was here
   iterator_init(&it);
   ^
src/command.c:245:6: warning: conflicting types for ‘iterator_destroy’ [enabled by     default]
 void iterator_destroy(iterator *it) {
      ^
src/command.c:49:6: note: previous implicit declaration of ‘iterator_destroy’ was here
      iterator_destroy(&it);
      ^

我的研究表明,这通常发生在函数在实际调用之后声明时。

但是我已经将函数的原型正确地放在了我的头文件中,所以想知道还有什么可能导致这种情况。我的 makefile 可能设置不正确吗?

我的应用程序源位于https://github.com/fish-guts/concurrent

【问题讨论】:

  • 请不要发布代码链接,发布实际代码。
  • 这是试图让程序编译的基本内容。只需注释掉代码块,直到它注释掉。然后开始添加位并阅读错误消息
  • 你是否包含过你的头文件?为获得最佳效果,请始终在第一行包含相应的标题。
  • Oli Charleswoth:我真的应该粘贴 500 行代码吗? Deduclicator,是的,我的头文件都包含在内。我在每个源文件中都包含我的“main.h”,并且所有相应的头文件都包含在 main.h

标签: c conflict implicit-declaration


【解决方案1】:

你在声明之前使用了函数。 C 语言允许隐式声明(最好不要使用它们),并自动声明函数 - 但隐式规则非常严格。稍后您定义函数,但它的原型与自动生成的不匹配。

可能的解决方案是:

  • 使用前定义函数
  • 使用前声明函数
  • 包括您的“command.h”,其中包含声明(这是特定情况下的最佳选择)

【讨论】:

  • 感谢您的回复。但正如我在帖子中所说:我正确地将函数的原型放在标题中。
  • 如果你提到的github上的代码是正确的,command.c不包含command.h,因此command.c翻译单元没有原型。
  • @Fish-Guts 从我在存储库中的代码中可以看到,您确实在标头中声明您的函数。在command.h 中,只有一个void interator_init(iterator *it);,这似乎是一个错字。破坏不见了。但是你真的应该发布代码 sn-ps 而不是链接到整个存储库。这会降低您在 SO 获得任何帮助的机会。
  • 感谢您的指点。从现在开始,我将根据自己的兴趣发布更多代码 sn-ps :)