C 警告:函数“fchmod”的隐式声明

发布于 2024-11-05 14:05:02 字数 595 浏览 3 评论 0原文

我有一个使用 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’

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

I have a function, createFile that uses fchmod:

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;
}

At the top of my file, I have the following includes:

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

When compiling: the compiler spits out:

warning: implicit declaration of function ‘fchmod’

I'm including all of the correct files, yet getting this warning. The program runs fine, even with the warning.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

七分※倦醒 2024-11-12 14:05:02

巧合的是,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

By a happy coincidence, your question is directly answered by the feature_test_macros(7) manpage:

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
救星 2024-11-12 14:05:02

您没有指定您使用的编译器或平台,但在我最近安装的 Linux 中,fchmod() 是在几个 #ifdef(__USD_BSD 和 __USE_XOPEN_EXTENDED)中定义的,但由几个 #ifdef 保护。

您不应该直接设置它们,而是通过 .尝试定义 _XOPEN_SOURCE_EXTENDED 或仅定义 _GNU_SOURCE 并重新编译(请注意,这些宏启用非标准功能,并且使用它们启用的功能可能会限制代码的可移植性)。

You didn't specify what compiler or platform you're using, but on my recent Linux installation, fchmod() is defined in but guarded by a couple of #ifdefs (__USD_BSD and __USE_XOPEN_EXTENDED).

You aren't supposed to set those directly, but rather via the _FOO_SOURCE macros in . Try defining _XOPEN_SOURCE_EXTENDED or just _GNU_SOURCE and recompiling (and note that these macros enable nonstandard functionality and use of the functionality they enable may limit the portability of your code).

深居我梦 2024-11-12 14:05:02

我在构建 uml 时遇到了这个错误。
只需在引发此错误的文件中添加此行:

#include "sys/stat.h"

我相信它会注意添加上述答案中定义的宏。

I have faced this error while building uml.
Just add this line in the file where this error is thrown:

#include "sys/stat.h"

I believe it will take care about adding the macros defined in the above answers.

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