具有“函数的隐式声明"的含义是什么?警告在C? [英] What are the implications of having an "implicit declaration of function" warning in C?

查看:90
本文介绍了具有“函数的隐式声明"的含义是什么?警告在C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题所指出的那样,发出隐式声明功能"警告的确切含义是什么?我们只是在gcc上增加了警告标志,并发现了许多此类警告的实例,我很好奇这在修复它们之前可能引起什么类型的问题?

此外,这为什么是警告而不是错误.gcc如何甚至能够成功链接此可执行文件?如您在下面的示例中看到的那样,可执行文件按预期运行.

以以下两个文件为例:

file1.c

  #include< stdio.h>int main(无效){funcA();返回0;} 

file2.c

  #include< stdio.h>无效funcA(void){puts("hello world");} 

编译&输出

  $ gcc -Wall -Wextra -c file1.c file2.cfile1.c:在函数"main"中:file1.c:3:警告:函数'funcA'的隐式声明$ gcc -Wall -Wextra file1.o file2.o -o test.exe$ ./test.exe你好,世界 

解决方案

如果函数的定义与隐式声明匹配(即,它返回 int 并且具有固定数量的参数,并且没有原型),而您总是使用正确数量和类型的参数来调用它,那么就没有负面影响(除了糟糕的,过时的样式).

即,在上面的代码中,就好像该函数被声明为:

  int funcA(); 

由于此不匹配,因此从 file1.c 调用 funcA()会调用未定义的行为,这意味着它可能会崩溃.在您的体系结构上,使用当前的编译器显然不会这样做-但是体系结构和编译器会发生变化.

GCC之所以能够链接它,是因为当函数类型更改时(代表...同样,在您当前的体系结构和当前的编译器上,尽管这很普遍),表示函数入口点的符号也不会更改.

正确声明您的函数是一件好事-如果仅出于其他原因,它可以使您为函数提供原型,这意味着如果您使用错误的数量或参数类型调用它,则编译器必须对其进行诊断

As the question states, what exactly are the implications of having the 'implicit declaration of function' warning? We just cranked up the warning flags on gcc and found quite a few instances of these warnings and I'm curious what type of problems this may have caused prior to fixing them?

Also, why is this a warning and not an error. How is gcc even able to successfully link this executable? As you can see in the example below, the executable functions as expected.

Take the following two files for example:

file1.c

#include <stdio.h>

int main(void)
{
   funcA();
   return 0;
}

file2.c

#include <stdio.h>

void funcA(void)
{
   puts("hello world");
}

Compile & Output

$ gcc -Wall -Wextra -c file1.c file2.c
file1.c: In function 'main':
file1.c:3: warning: implicit declaration of function 'funcA'

$ gcc -Wall -Wextra file1.o file2.o -o test.exe
$ ./test.exe
hello world

解决方案

If the function has a definition that matches the implicit declaration (ie. it returns int and has a fixed number of arguments, and does not have a prototype), and you always call it with the correct number and types of arguments, then there are no negative implications (other than bad, obsolete style).

ie, in your code above, it is as if the function was declared as:

int funcA();

Since this doesn't match the function definition, the call to funcA() from file1.c invokes undefined behaviour, which means that it can crash. On your architecture, with your current compiler, it obviously doesn't - but architectures and compilers change.

GCC is able to link it because the symbol representing the function entry point doesn't change when the function type changes (again... on your current architecture, with your current compiler - although this is quite common).

Properly declaring your functions is a good thing - if for no other reason than that it allows you to give your function a prototype, which means that the compiler must diagnose it if you are calling it with the wrong number or types of arguments.

这篇关于具有“函数的隐式声明"的含义是什么?警告在C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆