Header.h: 没有那个文件或目录

时间:2021-03-17 15:33:31

标签: c

我试图在编译时链接我的 c 文件和头文件,但收到以下错误: fatal error: header1.h: No such file or directory #include <header1.h>

问题是,我在每个 c 文件中都使用 #include <header1.h> 包含了头文件,并使用命令 gcc header1.h file1.c file2.c main.c -Wall -std=c99 进行了编译,但在每个 c 文件中仍然给我错误。我在下面的每个文件中都包含了代码的顶部。

header1.h:

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 

typedef struct microtreat{
    int id;                 
    char user[51];          
    char text[141];         
   
    struct microtreat*next;  
}treat;

main.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <header1.h>

文件 1:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <header1.h>

文件 2:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <header1.h>

我该如何解决这个错误?谢谢

2 个答案:

答案 0 :(得分:1)

试试

#include "header1.h"

当您使用 <> 包含时。预处理器在某些路径中搜索头文件,但如果你想在你的 c 文件的目录中包含文件,你应该使用 include ""

如果你想在其他目录中包含头文件,你可以用头文件所在的目录编译它:

gcc some_compilation_flags name_of_c_file_to_compile.c -iquote /path/to/the/header/directory

标志 -iquote 告诉编译器包含此目录以在其中查找包含文件

答案 1 :(得分:0)

在 C/C++ 中有两种包含头文件的方法。
#include <header.h> 在系统路径中查找标题
#include "header.h" 从文件的本地文件夹(包括标题)开始查找

这里有更详细的解释
What is the difference between #include <filename> and #include "filename"?