【问题标题】:Fatal error: No such file or directory for header file致命错误:头文件没有这样的文件或目录
【发布时间】:2015-03-05 04:24:17
【问题描述】:

我目前正在运行 Ubuntu(最新固件)并且有一堆我为我的 c 编程创建的目录。但是,编译时出现错误,告诉我找不到“file.h”头文件 在我的主文件夹“temp”中 目录如下

temp

然后:

src bin include assets Makefile

src 包含一个 .c 文件,它是:

#include "file.h"

int main(void)
{
    initscr();
    noecho();
    cbreak();

    char ch;
    FILE * ptr;
    ptr = fopen("input.txt","r");

    if (ptr == NULL)
    {
        mvprintw(0,0,"Error reading from file");
        exit(1);
    }

    while( ( ch = fgetc(ptr) ) != EOF )
    mvprintw(0,0,"%c",ch);
    refresh();

    fclose(ptr);
    return 0;

}

我的包含文件夹包含头文件(这给了我错误) 这就是 file.h 包含的内容...

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

我还有一个 makefile 如下:

all:
        gcc -Wall -pedantic -std=c99 src/file.c assets/input.txt -o bin/runMe

最后,资产包括一个 .txt 文件“input.txt”,程序只是应该从资产目录中的文本文件中读取一些字符。我试过使用“#include ”表示法,但这似乎没有帮助。我也尝试使用类似“#include”include/file.h“的东西,但也无济于事。

【问题讨论】:

  • -I path/to/your/include/folder 在编译器中的任何源文件可能有帮助之前。
  • 我已将其更改为:gcc -Wall -pedantic -std=c99 -Iinclude/include src/file.c assets/input.txt -o bin/runMe -lncurses
  • 但我仍然遇到同样的错误
  • 为什么要添加两个包含目录include/include?根据您的目录结构,您似乎只想要-Iinclude。您应该使用#include "file.h" 语法,而不是#include &lt;file.h&gt;

标签: c file makefile header ncurses


【解决方案1】:

如果file.h 位于temp 目录中,请在Makefile 中添加您的命令:

-Itemp

a.c:

#include <file.h>

file.h 位于包含它的文件的同一目录中时,您应该使用#include "file.h"

【讨论】:

    猜你喜欢
    • 2017-05-09
    • 2021-05-15
    • 2014-10-31
    • 2022-01-11
    • 2021-09-19
    • 2016-06-23
    • 2015-05-16
    相关资源
    最近更新 更多