c - 为什么 "implicit declaration of function"只是一个警告?

标签 c compilation warnings compiler-warnings linkage

这不是关于如何解决出现在 C 程序中的“函数的隐式声明”警告的问题,这已经是 answered已经很多次了。

我知道这是一个编译器警告,我想知道为什么这是一个警告而不是错误?如果编译器看不到该函数,那么在运行时调用该函数时会发生什么?链接器是否最终解决了这个问题?或者我们是否假设调用产生此类警告的函数的行为是未知的?

最佳答案

why is this a warning rather than an error?

因为有很多遗留代码,所以以这种方式编写。编译器错误会破坏它。

If the compiler cannot see the function, what happens when the function is called at runtime? Does the linker ultimately resolve this issue?

让我们看例子:

int main()
{
    foo();
    return 0;
}

在工作时,编译器会生成自己的函数签名,如 int foo(...) 并将使用它。顺便说一句,它可能会导致非常奇怪的错误。所以目标文件会包含这个函数的调用就可以了。当你尝试链接它时,你会得到一个错误:undefined reference to `foo'。但是,如果您有另一个具有 foo 定义的模块,链接器将通过名称找到它并链接它。

Or are we to assume that the behaviour of calling a function that produced such warning is unknown?

正如我所说,它可能会导致一些奇怪的错误。想象一下,您有类似 int i = foo() 的代码,而 foo 没有签名。在另一个模块中,您有以下内容:int * foo(){...}。在 64 位模式下构建应用程序时,您将仅将 64 位指针的 32 位放入 i。所以你可能会说实际上你的程序的行为可能是未知的。

关于c - 为什么 "implicit declaration of function"只是一个警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38516758/

相关文章:

java - 用户按下键时看不到消息

c++ - 无法获得适用于仅 header 库的警告

c++ - 如何在可能不存在的目录中创建文件?

c++ - 在 C++、Windows 7 中包含 OpenGL 库

c - 我如何编译这个非常大但无聊的 C 源代码?

python - 将Python编译成机器码可行吗?

r - tryCatch在for循环中

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

c - 替换字符串中的字符

c - fread'ing 和 fwrite'ing 包含指针的结构