[英]creating a makefile for multiple C files
我在为 5 个 c 程序创建 makefile 时遇到问题。 我现在每个 c 程序都能够自己编译,但是当我尝试制作 makefile 时,我在运行 make 时遇到此错误
Makefile:2: *** missing separator. Stop.
我试图放入 makefile 的文件名是 first.c、second.c、third.c、fourth.c 和 Fifth.c。 这是我现在的 makefile 文件:
program: first.o second.o third.o fourth.o fifth.o
gcc -o program first.o second.o third.o fourth.o fifth.o
first.o: first.c
gcc -c first.c
second.o: second.c
gcc -c second.c
third.o: third.c
gcc -c third.c
fourth.o: fourth.c
gcc -c fourth.c
fifth.o: fifth.c
gcc -c fifth.c
我们要给出多少次相同的答案,但这是我对同一件事的看法。
Makefile 应包含以下内容:
program: first.o second.o third.o fourth.o fifth.o
gcc -g -o program first.o second.o third.o fourth.o fifth.o
.c.o:
gcc -c -g $<
gcc 行之前的空格是单个制表符而不是空格。 如果您使用空格或忘记末尾的冒号,您将得到“缺少分隔符”。
该
.c.o:
是旧的方法,但它仍然有效。 这将创建一个默认规则,将 .c 文件转换为 .o 文件。 因为我包含了 -c 选项来只编译,所以我没有打扰 -o $@ 会说明将输出发送到哪里。
请注意,这解决了“缺少分隔符”的问题,并生成了最初要求的一个可执行文件。
这是它应该如何格式化:
all: first second third fourth fifth
first: first.c
gcc -o first first.c
second: second.c
gcc -o second second.c
third: third.c
gcc -o third third.c
fourth: fourth.c
gcc -o fourth fourth.c
fifth: fifth.c
gcc -o fifth fifth.c
或者:
all: first second third fourth fifth
%.o: %.c #Suppress default rule
%: %.c
gcc -o $@ $<
我有这样的makefile,但make只能在第一个程序中使用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.