简体   繁体   中英

Fedora 14 x86_64 implicit declaration of function 'sync'

I have the #include <unistd.h> header included in my source as specified in the man pages sync(2) . However when I compile my program I am getting the following warning.

./test.c:25:3: warning: implicit declaration of function 'sync'

Am I missing something here? Below is the source code for my small test program.

#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[]) {
  // Open file ./test.txt and append to it
  int fd = open("./test.txt", O_RDWR | O_CREAT | O_TRUNC, 0);
  if (fd < 0) {
    perror("Failure to open file: ");
    close(fd);
    return(1);
  }
  // Write Hello World\n\0 100 time to ./test.txt
  for (int i = 0; i < 100; ++i) {
    ssize_t bytes = write(fd, "Hello World\n", 12); // 12 for Null '\0'
    if (bytes != 12) {
      perror("Failure to write to file");
      close(fd);
      return(1);
    }
  }

  // Close the file for the exercise
  sync();
  close(fd);

  // This will allow lseek to go back
  fd = open("./test.txt", O_RDWR, 0);
  // This will not allow lseek to go back
  //fd = open("./test.txt", O_RDWR | O_APPEND, 0);
  if (fd < 0) {
    perror("Failure to open files: ");
    close(fd);
    return(1);
  }

  if (lseek(fd, -500, SEEK_END) == -1) {
    perror("Test failed: ");
    close(fd);
    return(1);
  }

  ssize_t bytes = write(fd, "\n\nHello Dog\n\n", 14);
  if(bytes != 14) {
    perror("Failure to write: ");
    close(fd);
    return(1);
  }

  write(1, "Done!!!\n", 9);
  close(fd);


return(0);
}

In a comment, I asked:

What are your compiler options? Did you set -D_XOPEN_SOURCE=700 (or some other value)? Did you specify -std=c99 instead of -std=gnu99 , or something like that?

To which the response was:

I used -std=c99 to get allow for variable initialization inside for loops. -std=gnu99 seemed to do the trick.

When you set -std=c99 , you have to explicitly request all symbols that are not part of C99 (at least to a first approximation). The simplest way to get all the symbols is to use -std=gnu99 , as you discovered. You can be more precise, if you wish, with options such as with this header (I call it posixver.h ):

#ifndef JLSS_ID_POSIXVER_H
#define JLSS_ID_POSIXVER_H

/*
** Include this file before including system headers.  By default, with
** C99 support from the compiler, it requests POSIX 2001 support.  With
** C89 support only, it requests POSIX 1997 support.  Override the
** default behaviour by setting either _XOPEN_SOURCE or _POSIX_C_SOURCE.
*/

/* _XOPEN_SOURCE 700 is loosely equivalent to _POSIX_C_SOURCE 200809L */
/* _XOPEN_SOURCE 600 is loosely equivalent to _POSIX_C_SOURCE 200112L */
/* _XOPEN_SOURCE 500 is loosely equivalent to _POSIX_C_SOURCE 199506L */

#if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE)
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600   /* SUS v3, POSIX 1003.1 2004 (POSIX 2001 + Corrigenda) */
#else
#define _XOPEN_SOURCE 500   /* SUS v2, POSIX 1003.1 1997 */
#endif /* __STDC_VERSION__ */
#endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */

#endif /* JLSS_ID_POSIXVER_H */

On Linux, you might prefer to use _XOPEN_SOURCE set to 700 (for POSIX 2008), but many of the machines I work on do not have good support for that.

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