makefile fatal error: no such file or directory, but the file is in current directory

makefile fatal error: no such file or directory, but the file is in current directory

我正在尝试为一些 .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)位于同一目录中。

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

您的 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 header 文件。链接器将根据针对 lib.h.

的编译从 lib.c 引入所需的函数

另请注意,带方括号 <> 的包含文件仅适用于系统 header。用户定义的 header 文件应使用引号包含在内:

#include "lib.h"

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