简体   繁体   English

Makefile 用于多个 C 程序

[英]Makefile for multiple C programs

I've got a task to do a makefile of 5 different c programs.我有一个任务要做 makefile 5 个不同的 c 程序。 Let's call them 1.c, 2.c, 3.c, 4.c, 5.c The code i've got is:我们称它们1.c, 2.c, 3.c, 4.c, 5.c我得到的代码是:

CC = gcc
CFLAGS = -Wall
LDFLAGS =
OBJFILES = 1.o 2.o 3.o 4.o 5.o 
TARGET = 1 2 3 4 5

all: $(TARGET)
$(TARGET): $(OBJFILES)
    $(CC) $(CFLAGS) -o $(TARGET) $(OBJFILES) $(LDFLAGS)
clean:
    rm -f $(OBJFILES) 

And the feedback is that I should do something with static template.反馈是我应该用 static 模板做点什么。 How should I approach this problem?我应该如何处理这个问题?

You should let Make use its default rules.你应该让 Make 使用它的默认规则。 Your entire Makefile can be as simple as:你的整个 Makefile 可以很简单:

CC = gcc
CFLAGS = -Wall

all: 1 2 3 4 5 

But if you are willing to require the user to type make 1 and make 2 , etc., you can just delete the makefile completely.但如果你愿意要求用户输入make 1make 2等,你可以完全删除 makefile。 Let the user specify CC and CFLAGS in their environment if needed/desired.如果需要/需要,让用户在他们的环境中指定 CC 和 CFLAGS。

I don't know what you mean by a static template, but your Makefile does not work.我不知道您所说的 static 模板是什么意思,但是您的 Makefile 不起作用。 Try this change instead:试试这个改变:

$(TARGET): $(OBJFILES)
        $(CC) $(CFLAGS) -o $@ $@.o $(LDFLAGS)

The drawback is that it compiles all five programs even if only one has to be rebuilt.缺点是它会编译所有五个程序,即使只需要重建一个程序也是如此。

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

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