简体   繁体   English

是否在stdlib.h之外的其他任何地方声明了exit()函数?

[英]Is exit() function declared anywhere else besides stdlib.h?

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> . 经过一些搜索,我意识到该示例缺少语句#include <stdlib.h> Where was then exit() function declared? 然后在哪里声明了exit()函数? Library stdio.h does not declare it. stdio.h没有声明它。 Neither does my code. 我的代码也没有。 If it is supported by compiler, why then it gives a warning? 如果编译器支持它,为什么会给出警告? Also, why is it redefined in stdlib.h ? 另外,为什么要在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;    
}

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. 即使您不包含标准标头,GCC也会知道这些标头的内容,并抱怨函数的隐式(或推断)声明与包含标头时的含义不完全相同。

By inference, the type of exit() is: 通过推断, exit()的类型为:

extern int exit();  // Indeterminate argument list

This is not the same as the official declaration: 这与官方声明不同:

extern void exit(int);

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. [有一个狡猾的单词'sufficient',因为当未注释exit()的声明时,此代码在编译时不会发出警告,但在缺少声明时会生成警告。

extern void exit();
int main(int argc, char **argv)
{
    if (argc > 1 && argv[0] != 0)
        exit(1);
    return(0);
}

End of weasel words.] 黄鼠狼词的结尾。]


Note: pre-standard C heavily used implicit function declarations. 注意:标准C大量使用了隐式函数声明。 C89 started to encourage the use of proper definitions, in part by ensuring that every standard function had a header that declared it. C89开始鼓励使用正确的定义,部分是通过确保每个标准函数都有一个声明它的标头来实现的。 (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. (通过确保在头文件中声明了它定义的所有功能,POSIX也提供了帮助。)C99进一步说明,标准前和C89对函数的“隐式int”解释不再有效。 GCC is now helping you get around to fixing your code by identifying the functions. 现在,GCC正在帮助您通过识别功能来修正代码。 You can use the options: 您可以使用以下选项:

-Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition

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. 如果您(像我一样)使用尚未过时的现代C编码标准的过时代码库,则可以帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM