【问题标题】:Error message when trying to use Open Function call尝试使用 Open Function 调用时出现错误消息
【发布时间】:2019-09-12 19:38:35
【问题描述】:

作为作业的一部分,我需要为 XV6 创建一个小型 c 程序。这个程序必须创建一个文件,写入它,然后关闭它。

当我尝试用我的文件编译 xv6 时,我收到两条错误消息。

HWC2.c:13:82: error: ‘O_CREAT’ undeclared (first use in this function)
 fd = open("/home/kyle/Desktop/Projects/'Assignment 2'/xv6/Tom.txt", O_RDWR | O_CREAT);
                                                                              ^
HWC2.c:13:14: error: called object ‘open’ is not a function or function pointer
 fd = open("/home/kyle/Desktop/Projects/'Assignment 2'/xv6/Tom.txt", O_RDWR | O_CREAT);

我是 C 编程的新手,所以我仅限于通过 google 搜索可以找到的内容,并且没有任何帮助。相反,O_CREAT 我确实尝试使用 write。

#include "types.h"
#include "stat.h"
#include "user.h"

int open;
int fd;

int main (void)
{

    printf(1,"My Name\n");
    fd = open("/home/kyle/Desktop/Projects/Assignment 2/xv6/Tom.txt", O_RDWR | O_CREAT);

    write(fd, "1 2 3 4", 7);
    close(fd);

}

我需要它来编译,打印我的名字,创建文件,写入文件,然后保存并关闭文件

【问题讨论】:

  • 你忘了#include "fcntl.h"。在这个文件中,它是 O_CREATE 而不是 O_CREAT
  • 谢谢。我已经在编译中添加并进一步制作了它,但是 fcntl.h 存在问题。包含在 /usr/include/fcntl.h:25:0 中的文件中,来自 HW2.c:4:/usr/include /features.h:424:25: 致命错误: sys/cdefs.h: No such file or directory #include ^ 编译终止。 : 目标“HW2.o”的配方失败 make: *** [HW2.o] 错误 1 ​​我检查了 sys 文件夹并且文件在那里。
  • 我不确定你是否选择了好的标题。查看 fcntl.h info xv6 repo:它只有几行。你的编译链是如何配置的?
  • 我能够修复它。感谢您的帮助。我有 当我应该完成你发布的内容并在 fcntl.h 周围使用 "" 时。

标签: c system-calls xv6


【解决方案1】:

尝试包括

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

对于打印名称,您可以尝试:

printf("%s\n", "My Name");

【讨论】:

    猜你喜欢
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 2018-06-28
    • 1970-01-01
    相关资源
    最近更新 更多