C 警告 : implicit declaration of function ‘fchmod’

标签 c compiler-construction compiler-warnings

我有一个使用 fchmod 的函数 createFile:

int createFile(char *pFileName) {
   int ret;

   if ((ret = open(pFileName, O_RDWR | O_CREAT | O_TRUNC)) < 0)
      errorAndQuit(2);

   fchmod(ret, S_IRUSR | S_IWUSR);
   return ret;
}

在我的文件顶部,我有以下内容:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

编译时:编译器吐出:

warning: implicit declaration of function ‘fchmod’

我包含了所有正确的文件,但收到此警告。该程序运行良好,即使有警告。

最佳答案

巧合的是,feature_test_macros(7) 联机帮助页直接回答了您的问题:

Specification of feature test macro requirements in manual pages
   When a function requires that a feature test macro is
   defined, the manual page SYNOPSIS typically includes a note
   of the following form (this example from the chmod(2) manual
   page):

          #include <sys/stat.h>

          int chmod(const char *path, mode_t mode);
          int fchmod(int fd, mode_t mode);

      Feature Test Macro Requirements for glibc (see
      feature_test_macros(7)):

          fchmod(): _BSD_SOURCE || _XOPEN_SOURCE >= 500

   The || means that in order to obtain the declaration of
   fchmod(2) from <sys/stat.h>, either of the following macro
   definitions must be made before including any header files:

          #define _BSD_SOURCE
          #define _XOPEN_SOURCE 500     /* or any value > 500 */

   Alternatively, equivalent definitions can be included in the
   compilation command:

          cc -D_BSD_SOURCE
          cc -D_XOPEN_SOURCE=500        # Or any value > 500

关于C 警告 : implicit declaration of function ‘fchmod’ ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5918539/

相关文章:

rust - 如何抑制源自宏的Clippy警告?

c# - 为什么没有关于身份分配的警告?

c - 如何处理 execvp 中的错误?

c - 如何更安全地编写此 C 片段?

visual-studio-2010 - 在64位系统上编译为32位系统-兼容性

c# - 需要在命令行上 msbuild 低版本的 C# 项目,没有 Visual Studio

c# - 从输入字符串中释放 .exe 文件作为代码

c - 为什么 "int main(anything_you_type)"没有产生任何错误?

c++ - Excel 公式计算

java - 使用泛型返回列表的接口(interface)会导致警告