我尝试使用其他两个最相关的主题来解决此问题: Makefile 错误:没有规则来创建目标 Makefile:没有创建目标的规则。停止
但似乎都没有解决我的问题。
我不确定我的代码哪里出了问题,因为我的导师验证了该代码在他那一端有效。
我的 makefile 的代码是:
TARGET = demo
FILES = test.c
OBJS = $(FILES:.c=.o)
ASMS = $(FILES:.c=.s)
all: $(TARGET)
$(TARGET): $(OBJS)
gcc -o $@ $^
%.o: %.c
gcc -c $< -o $@
%.s: %.c
gcc -S -masm=intel $< -o $@
asm: $(ASMS)
run: $(TARGET)
./$(TARGET)
clean:
rm -f $(TARGET) $(OBJS) $(ASMS)
但是,当我尝试执行“make run”时,它会产生结果
make: *** No rule to make target 'run'. Stop.
我尝试编译的程序的实际代码只有 4 行长。我不认为这是问题的原因
#include <stdio.h>
char *msg = "Exam Lab 1";
int main(int argc, char *argv[]) {
printf("%s\n", msg);
}
这是我正在运行的目录的内容
make
来自:
您的 makefile 没有正确的名称。
默认情况下,
make
会查找名为 makefile
或 Makefile
的文件。你的名字是Makefile.txt
,所以make
找不到它。
将名称更改为
makefile
或 Makefile
,或使用 -f
选项指定 makefile 名称,例如。 make -f Makefile.txt
。
从您提供的图像中可以清楚地看出您已将文件命名为
Makefile.txt
。正如 @dbush 所说,makefile MUST 命名为 Makefile
或 makefile
。这些是默认情况下唯一查找的文件名。
要么这样,要么你必须跑
make -f Makefile.txt
。
在我的例子中,
Makefile
依赖于另一个外部*.a
库文件已经构建并存在。
我的意思是,在 Makefile 世界中甚至说外部依赖项称为“目标”。
因此,确实存在“没有规则来制定目标”;-)