【问题标题】:O_WRONLY undeclared (first use in this function)O_WRONLY 未声明(在此函数中首次使用)
【发布时间】:2013-05-19 13:08:51
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
char data [ 6 ];
main ( ) {
int len;
desc = open ( "Resultat", O_WRONLY | O_CREAT | O_EXCL, 0666 );
if ( desc != -1 ) {
len = write ( desc, &data, sizeof ( data ) );
if ( len != sizeof ( data ) )
printf ( "ERROR" );
} }

这是我的代码,我收到了错误

O_WRONLY undeclared (first use in this function)
O_CREAT undeclared (first use in this function)
O_EXCL undeclared (first use in this function)

我该如何解决这个问题?

【问题讨论】:

  • 你在哪个平台上?一旦我将 int 数据类型分配给 desc,这将为我在 CentOS 5 和 CentOS 6 上构建
  • #include ?
  • ubuntu 9.10,我添加了#include 但仍然是同样的错误..
  • 我的机器 (mac) 显示在 sys/fcntl.h 中定义的 O_WRONLY,请尝试包含它。
  • 一个名为test 的程序的问题在于shell 也有一个名为test(又名[)的内置函数。除非您运行 ./test(即使 .$PATH 上),否则它会优先于您的程序调用。

标签: c linux ubuntu


【解决方案1】:

@Kevin 是对的。在我的 Arch 安装中,根据man fcntl.h,您需要#include &lt;fcntl.h&gt; 才能访问O_WRONLY

要使用open(),还需要#include &lt;sys/stat.h&gt;

【讨论】:

    【解决方案2】:

    我已在我的机器(Ubuntu 12.0.4)中尝试过此代码。但我没有收到像你这样的错误消息。

    根据open() 的手册页,您可能缺少#include &lt;sys/stat.h&gt;

    【讨论】:

    • 确实如此。您应该始终阅读您正在调用的任何函数的手册页,并确保您有 #included 它指定的任何标题。对于open,所需的标头是&lt;sys/types.h&gt;&lt;sys/stat.h&gt;&lt;fcntl.h&gt;
    【解决方案3】:

    open(2) 的手册页:

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    int open(const char *pathname, int flags);
    int open(const char *pathname, int flags, mode_t mode);
    

    请确认您拥有所需的每一项。

    【讨论】:

      猜你喜欢
      • 2012-05-19
      • 2014-04-22
      • 2012-05-07
      • 2012-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多