简体   繁体   中英

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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