【问题标题】:makefile fatal error: no such file or directory, but the file is in current directorymakefile 致命错误:没有这样的文件或目录,但文件在当前目录中
【发布时间】:2017-08-18 01:50:54
【问题描述】:

我正在尝试为一些 .c 和 .h 文件制作一个非常简单的 makefile,所有这些文件都在当前目录中。我承认我不完全理解makefile;这是我目前所拥有的:

prog3 : prog3.c prog3.h lib.o
    gcc -c prog3.c

lib.o : lib.c lib.h
    gcc -c lib.c

当我使用命令 make 时,我会收到以下消息:

prog3.c:5:17: fatal error: lib.c: No such file or directory  
compilation terminated.  
makefile:2: recipe for target 'prog3' failed  
make: *** [prog3] Error 1

但是,lib.c 文件与所有其他文件(prog3.cprog3.hlib.h)位于同一目录中。

我发现了很多关于这个特定错误的问题,但没有一个与 PWD 中的文件有关。我做错了什么?

【问题讨论】:

  • 尝试自己在终端中按顺序运行命令。先是gcc -c lib.c,然后是gcc -c prog3.c。它是否阐明了问题?

标签: c makefile directory header-files fatal-error


【解决方案1】:

您的 makefile 看起来不错。 makefile 的基本结构包含以下模式:

<build object> : <list of dependencies>
       command to execute to build the object if dependencies have changed
           ...
       command to execute to build the object if dependencies have changed

您的 makefile 正在尝试编译 prog3.c,这是它应该做的。您在 prog3.c 中有一个编译错误,看起来您正在尝试 #include "lib.c",但我猜您是在第 15 行执行此操作:

#include <lib.c>

如果这是你正在做的,这是错误的。 C 实现文件不应包含其他 .c 文件(极少数例外)。您应该只包含 .h 头文件。链接器将根据针对 lib.h 的编译从 lib.c 中引入所需的函数。

另请注意,带括号 的包含文件仅用于系统标头。用户定义的头文件应该用引号括起来:

#include "lib.h"

如果这不能解决您的编译错误,请编辑您的问题以提供 prog3.c 和 lib.c 文件(如果您希望检查这些文件)。

【讨论】:

  • 问题是 lib.h 上的引号,我想!我有括号。菜鸟的错误...非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 2021-05-15
  • 2014-10-31
  • 2022-01-11
  • 2021-09-19
相关资源
最近更新 更多