Makefile通用二进制编译 [英] Makefile generic binaries compilations

查看:70
本文介绍了Makefile通用二进制编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组共享相同Makefile规则模式的源文件:

I have a set of source files that share the same Makefile rule pattern:

bin1: bin1.o libfoo.a
    $(LDENV) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

bin2: bin2.o libfoo.a
    $(LDENV) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

bin3: bin3.o libfoo.a
    $(LDENV) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

...

现在,我该如何重构它以避免一遍又一遍地重复相同的规则?如果将文件扩展名添加到二进制文件中,这很容易:

Now, how may I refactor this to avoid repeating the same rule over and over? This is easy if a file extension is added to the binaries:

%.out: %.o libfoo.a
    $(LDENV) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

但是可以不使用一个做同样的事情吗?

But is it possible to do the same without using one?

推荐答案

您可以使用静态模式规则:

bin1 bin2 bin3: %: %.o libfoo.a
    $(LDENV) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

这样更干净,您可以确定%仅匹配bin1bin2bin3目标(我想这是您想要的).

This way this is cleaner and you are sure that the % only match bin1, bin2 and bin3 targets (I think this is want you want).

这篇关于Makefile通用二进制编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆