C Fedora 14 x86_64隐式函数声明';同步';

C Fedora 14 x86_64隐式函数声明';同步';,c,posix,fedora,C,Posix,Fedora,我的源代码中包含了手册页sync(2)中指定的#include标题。然而,当我编译我的程序时,我得到以下警告 ./test.c:25:3:警告:函数“sync”的隐式声明 我是不是遗漏了什么?下面是我的小测试程序的源代码 #include <fcntl.h> #include <unistd.h> #include <stdio.h> int main(int argc, char *argv[]) { // Open file ./test.txt a

我的源代码中包含了手册页
sync(2)
中指定的
#include
标题。然而,当我编译我的程序时,我得到以下警告

./test.c:25:3:警告:函数“sync”的隐式声明

我是不是遗漏了什么?下面是我的小测试程序的源代码

#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);
}
#包括
#包括
#包括
int main(int argc,char*argv[]){
//打开文件./test.txt并附加到它
int fd=open(“./test.txt”,O|RDWR | O|CREAT | O|TRUNC,0);
如果(fd<0){
perror(“无法打开文件:”);
关闭(fd);
申报表(1);
}
//将Hello World\n\0 100时间写入。/test.txt
对于(int i=0;i<100;++i){
ssize_t bytes=write(fd,“Hello World\n”,12);//12表示空'\0'
如果(字节数!=12){
perror(“未能写入文件”);
关闭(fd);
申报表(1);
}
}
//关闭该练习的文件
sync();
关闭(fd);
//这将允许lseek返回
fd=打开(“./test.txt”,O_RDWR,0);
//这将不允许lseek返回
//fd=open(“./test.txt”,O|RDWR | O|u APPEND,0);
如果(fd<0){
perror(“无法打开文件:”);
关闭(fd);
申报表(1);
}
if(lseek(fd,-500,SEEK_END)=-1){
perror(“测试失败:”);
关闭(fd);
申报表(1);
}
ssize_t bytes=write(fd,“\n\nHello Dog\n\n”,14);
如果(字节数!=14){
佩罗尔(“未能书写:”);
关闭(fd);
申报表(1);
}
写(1,“完成!!!\n”,9);
关闭(fd);
返回(0);
}
在评论中,我问:


你的编译器选项是什么?您是否设置了
-D\u XOPEN\u SOURCE=700
(或其他值)?您是否指定了
-std=c99
,而不是
-std=gnu99
,或者类似的东西

答复如下:

我使用
-std=c99
来获得允许在for循环内部进行变量初始化
-std=gnu99
似乎起到了作用

设置
-std=c99
时,必须明确请求不属于c99的所有符号(至少是一级近似值)。获取所有符号的最简单方法是使用
-std=gnu99
,正如您所发现的那样。如果您愿意,您可以更精确地使用诸如此标题之类的选项(我称之为
posixver.h
):


在Linux上,您可能更喜欢使用设置为700的
\u XOPEN\u SOURCE
(对于POSIX 2008),但我使用的许多机器都不支持这一点。

您的编译器选项是什么?您是否设置了
-D\u XOPEN\u SOURCE=700
(或其他值)?您是否指定了
-std=c99
而不是
-std=gnu99
,或者类似的东西?@Jonathan Leffler我使用std=c99来获得for循环内部的变量初始化-std=gnu99似乎起到了作用。请在下面提供您的答案,以便我将其标记为问题的解决方案。
#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 */