假设我们有客户端-服务器应用程序,由一个 makefile 编译。服务器使用 libtask 为并行客户端服务。客户端将 ncurses 用于某些图形。目录树如下所示:
./
--bin/
--obj/
--src/
----client/*.c
----server/*.c
--makefile
所以,这里是makefile。我们可以简化它吗?我阅读了一些关于 stackoverflow 的相关问题,我认为它几乎是理想的,除了链接二进制文件,当我们可以看到重复的命令时。一些关于风格/跨平台工作的建议会很有用。谢谢!
.PHONY: all clean
TASKLIB = -ltask
CURSESLIB = -lncurses
CFLAGS = -Wall -Werror -pedantic
bin = server client
obj_dir = obj
bin_dir = bin
ssource := src/server
csource := src/client
search_wildcard s := $(addsuffix /*.c, $(ssource))
search_wildcard c := $(addsuffix /*.c, $(csource))
sobjs = $(addprefix $(obj_dir)/, $(patsubst %.c, %.o, $(notdir $(wildcard $(search_wildcard s)))))
cobjs = $(addprefix $(obj_dir)/, $(patsubst %.c, %.o, $(notdir $(wildcard $(search_wildcard c)))))
all: $(bin)
@echo "done!"
server: CFLAGS+=$(TASKLIB)
client: CFLAGS+=$(CURSESLIB)
clean:
rm $(sobjs) $(cobjs) $(obj_dir)/*.d $(bin_dir)/client $(bin_dir)/server
VPATH := $(ssource) $(csource)
server: $(sobjs)
@test -d $(bin_dir) || mkdir $(bin_dir)
@echo "Linking $(@F)..."
@$(CC) $< $(CFLAGS) -o $(bin_dir)/$@
client: $(cobjs)
@test -d $(bin_dir) || mkdir $(bin_dir)
@echo "Linking $(@F)..."
@$(CC) $< $(CFLAGS) -o $(bin_dir)/$@
$(obj_dir)/%.o: %.c
@test -d $(obj_dir) || mkdir $(obj_dir)
@echo "Compiling $(@F)..."
@$(CC) $< $(CFLAGS) -c -MD -o $@
include $(wildcard $(obj_dir)/*.d)