Warning C: implicit declaration of fchmod function

I have a function, createFile, which 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:

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

At compilation: the compiler spits out:

warning: implicit declaration of functionfchmod

I include all the correct files, but I get this warning. The program works fine, even with a warning.

+3
source share
3 answers

By a happy coincidence, feature_test_macros(7)manpage directly answers your question :

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
+4
source

You did not specify which compiler or platform you are using, but in my last install of Linux fchmod () is defined but password protected #ifdefs (__USD_BSD and __USE_XOPEN_EXTENDED).

, _FOO_SOURCE. _XOPEN_SOURCE_EXTENDED _GNU_SOURCE ( , , ).

+2

I encountered this error while creating uml.
Just add this line to the file that this error is called into:

#include "sys/stat.h"

I believe he will take care of adding the macros defined in the answers above.

0
source

All Articles