除了 stdlib.h 之外,是否在其他地方声明了 exit() 函数?
当尝试编译下面的示例时,我收到一条警告:
>gcc -o file file.c
file.c: In function ‘main’:
file.c:12: warning: incompatible implicit declaration of built-in function ‘exit’
经过一番搜索,我意识到该示例缺少语句 #include
。 那么 exit()
函数是在哪里声明的呢?库 stdio.h
没有声明它。我的代码也没有。如果编译器支持,为什么会发出警告?另外,为什么在 stdlib.h
中重新定义它?
例子:
#include <stdio.h>
int main()
{
char *fn = "./test.txt";
FILE *fp;
if((fp = fopen(fn, "w"))==NULL)
{
printf("Cannot open file '%s' for writing.\n", fn);
exit(1);
}
fprintf(fp, "Hello, world!\n");
if(fclose(fp)==0)
printf("File '%s' closed successfully.\n", fn);
else
printf("Error closing file '%s'.\n", fn);
return 0;
}
When trying to compile the example below, I received a warning:
>gcc -o file file.c
file.c: In function ‘main’:
file.c:12: warning: incompatible implicit declaration of built-in function ‘exit’
After some search, I realized that example was missing statement #include <stdlib.h>
.
Where was then exit()
function declared? Library stdio.h
does not declare it. Neither does my code. If it is supported by compiler, why then it gives a warning? Also, why is it redefined in stdlib.h
?
Example:
#include <stdio.h>
int main()
{
char *fn = "./test.txt";
FILE *fp;
if((fp = fopen(fn, "w"))==NULL)
{
printf("Cannot open file '%s' for writing.\n", fn);
exit(1);
}
fprintf(fp, "Hello, world!\n");
if(fclose(fp)==0)
printf("File '%s' closed successfully.\n", fn);
else
printf("Error closing file '%s'.\n", fn);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使您不包含标准头文件,GCC 也知道它们的内容,并且当函数的隐含(或推断)声明与包含头文件时的声明不够相同时,GCC 会发出抱怨。
由此推断,
exit()
的类型是:这与官方声明不一样:
因此出现警告。习惯了;修复代码。
[之所以存在“足够”这个狡猾的词,是因为当
exit()
的声明没有被注释掉时,此代码编译时不会发出警告,但在缺少该声明时会生成警告。狡猾的话结束了。]
注意:标准之前的 C 大量使用隐式函数声明。 C89 开始鼓励使用正确的定义,部分方法是确保每个标准函数都有一个声明它的标头。 (POSIX 还通过确保它定义的所有函数都在标头中声明来提供帮助。)C99 更进一步,指出预标准和 C89 对函数的“隐式 int”解释不再有效。 GCC 现在可以通过识别函数来帮助您修复代码。 则可以使用以下选项来帮助您发现问题。
如果您(像我一样)使用尚未按照现代 C 编码标准进行彻底修改的过时代码库,
GCC knows about the contents of the standard headers even when you don't include them, and complains when the implied (or inferred) declaration of the function isn't sufficiently the same as what it would be if the header is included.
By inference, the type of
exit()
is:This is not the same as the official declaration:
Hence the warning. Get used to it; fix the code.
[The weasel word 'sufficiently' is there because this code compiles without warning when the declaration of
exit()
is not commented out, but generates the warning when it is missing.End of weasel words.]
Note: pre-standard C heavily used implicit function declarations. C89 started to encourage the use of proper definitions, in part by ensuring that every standard function had a header that declared it. (POSIX helped too by ensuring that all the functions it defines are declared in a header.) C99 moved one step further by saying that the pre-standard and C89 'implicit int' interpretation of functions was no longer valid. GCC is now helping you get around to fixing your code by identifying the functions. You can use the options:
to help you catch the problems if (like me) you work on antiquated code bases that have not been overhauled to modern C coding standards.