【问题标题】:common.h: No such file or directory (Ubuntu gcc) [duplicate]common.h:没有这样的文件或目录(Ubuntu gcc)[重复]
【发布时间】:2016-08-31 05:00:13
【问题描述】:

当我试图编译这样的 C 程序时:

1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/time.h>
4 #include <assert.h>
5 #include "common.h"
6
7 int
8 main(int argc, char *argv[])
9 {
10 if (argc != 2) {
11 fprintf(stderr, "usage: cpu <string>\n");
12 exit(1);
13 }
14 char *str = argv[1];
15 while (1) {
16 Spin(1);
17 printf("%s\n", str);
18 }
19 return 0;
20 }

我收到一个错误

CPU.c:5:19: fatal error: common.h: No such file or directory

我已经更新了我的 gcc 编译器,所以我无法弄清楚为什么缺少“Common.h”

【问题讨论】:

  • 这是来自操作系统书籍的示例。它不包括 common.h。代替 common.h 包括:#include &lt;unistd.h&gt;,代替 Spin 使用 sleep 函数。

标签: c++ ubuntu gcc


【解决方案1】:

common.h 不是 C 标准库的一部分,它是一个第三方头文件(是你的吗?)。

您需要将common.h与您发布代码的文件放在同一目录下,否则编译器将无法在没有帮助的情况下找到它。

“一些帮助”是指包含路径。当您包含一个文件时,如果您使用引号 ("somefile.h") 而不是尖括号,编译器将在它知道的预定义位置以及当前目录(它正在编译的文件的目录)中搜索它(&lt;somefile.h&gt;)。

因此,您要么需要将标头放在其中一个位置,要么使用 gcc 中的 -I 标志定义一个新的(自定义包含目录),如下所示:

gcc -Ipath/to/your/dir/here myfile.c 

【讨论】:

    猜你喜欢
    • 2019-02-19
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    • 2021-12-13
    • 2012-02-12
    相关资源
    最近更新 更多