简体   繁体   English

为多个 C 文件创建一个 makefile

[英]creating a makefile for multiple C files

Im having trouble creating a makefile for 5 c programs.我在为 5 个 c 程序创建 makefile 时遇到问题。 I now that each of the c programs are able to compile on its own but when I try to make a makefile I get this error when running make我现在每个 c 程序都能够自己编译,但是当我尝试制作 makefile 时,我在运行 make 时遇到此错误

Makefile:2: *** missing separator.  Stop.

The file names that I am trying to put into the makefile are first.c, second.c, third.c, fourth.c, and fifth.c.我试图放入 makefile 的文件名是 first.c、second.c、third.c、fourth.c 和 Fifth.c。 This is what I have for my makefile as of right now:这是我现在的 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

How many times are we going to give the same answer but here is my take on the same thing.我们要给出多少次相同的答案,但这是我对同一件事的看法。

The Makefile should consist of the following: 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 $<

Where the spaces before the gcc lines are a single tab not spaces. gcc 行之前的空格是单个制表符而不是空格。 If you use spaces or forget the colon at the end, you will get "missing separator".如果您使用空格或忘记末尾的冒号,您将得到“缺少分隔符”。

The

.c.o: 

is the old way to do it but it still works.是旧的方法,但它仍然有效。 This creates a default rule to turn .c files into .o files.这将创建一个默认规则,将 .c 文件转换为 .o 文件。 Since I include the -c option to only compile, I did not bother with the -o $@ that would say where to send the output.因为我包含了 -c 选项来只编译,所以我没有打扰 -o $@ 会说明将输出发送到哪里。

Observe, this addresses the "missing separator" issue and produces the one executable that was originally asked for.请注意,这解决了“缺少分隔符”的问题,并生成了最初要求的一个可执行文件。

This is how it should be formatted:这是它应该如何格式化:

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

Alternatively:或者:

all: first second third fourth fifth

%.o: %.c    #Suppress default rule
%: %.c
    gcc -o $@ $<

我有这样的makefile,但make只能在第一个程序中使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM