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

发布于 2024-10-21 16:11:40 字数 826 浏览 3 评论 0原文

当尝试编译下面的示例时,我收到一条警告:

>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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£烟消云散 2024-10-28 16:11:40

即使您不包含标准头文件,GCC 也知道它们的内容,并且当函数的隐含(或推断)声明与包含头文件时的声明不够相同时,GCC 会发出抱怨。

由此推断,exit() 的类型是:

extern int exit();  // Indeterminate argument list

这与官方声明不一样:

extern void exit(int);

因此出现警告。习惯了;修复代码。


[之所以存在“足够”这个狡猾的词,是因为当 exit() 的声明没有被注释掉时,此代码编译时不会发出警告,但在缺少该声明时会生成警告。

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

狡猾的话结束了。]


注意:标准之前的 C 大量使用隐式函数声明。 C89 开始鼓励使用正确的定义,部分方法是确保每个标准函数都有一个声明它的标头。 (POSIX 还通过确保它定义的所有函数都在标头中声明来提供帮助。)C99 更进一步,指出预标准和 C89 对函数的“隐式 int”解释不再有效。 GCC 现在可以通过识别函数来帮助您修复代码。 则可以使用以下选项来帮助您发现问题。

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

如果您(像我一样)使用尚未按照现代 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:

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.

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. 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:

-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文