在编译为gnu99时,获取"隐含的函数声明'fcloseall'在C99中无效"

Ema*_* Ey 6 c c99 clang

考虑以下C代码:

#include <stdio.h>
#include <stdlib.h>

void fatal(const char* message){
 /*
  Prints a message and terminates the program.
  Closes all open i/o streams before exiting.
 */
 printf("%s\n", message);
 fcloseall();
 exit(EXIT_FAILURE);
}
Run Code Online (Sandbox Code Playgroud)

我正在使用clang 2.8编译: clang -Wall -std=gnu99 -o <executable> <source.c>

得到: implicit declaration of function 'fcloseall' is invalid in C99

这是真的,但我明确地编译为gnu99 [应该支持fcloseall()],而不是c99.虽然代码运行,但我不喜欢在编译时有未解决的警告.我怎么解决这个问题?

编辑:更正了tipo.

CB *_*ley 4

要在包含标准标头时包含非标准扩展,您需要定义适当的功能测试宏。在这种情况下_GNU_SOURCE应该有效。

#define _GNU_SOURCE
#include <stdio.h>
Run Code Online (Sandbox Code Playgroud)

-std=gnu99这与启用语言扩展无关,与库扩展无关。

  • 还应该注意的是,“fcloseall”没有合法用途,就像假设的“freeall”函数没有合法用途一样。释放给定类型的“所有”资源是一个编程错误;它们应该由拥有它们的代码单独释放,或者根本不释放。`fflush(0)` 将是一种完全安全且可移植的方式,以确保在不违反此原则的情况下写入所有数据,但随后对 `exit` 的调用已经做了正确的事情,所以这整个问题是 Cargo Cult 的问题编程... (4认同)
  • @Let_Me_Be:您能解释一下或提供支持参考吗?谢谢。 (3认同)