根据目标更改Makefile变量值 [英] Change Makefile variable value depending on a target

查看:205
本文介绍了根据目标更改Makefile变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不精通Makefile,但是习惯于简单的Makefile.现在,我手头上有任务.

I am not proficient with makefiles, but am used to simple ones. Right now, I have a task on hand.

我需要编译测试应用程序并将其与其他库和基于给定目标的其他包含路径链接在一起.如果目标是TARGET1,则链接到LIB1并在编译过程中包含INCLUDEPATH1.同样,如果给定的目标是TARGET2,则在CFLAGS中使用INCLUDEPATH2进行编译,并与LIB2链接.

I need to compile and link a test application with a different library and different include path based on the given target. If target is TARGET1, then link against LIB1 and include INCLUDEPATH1 during compilation. Similarly, if given target is TARGET2, then compile with INCLUDEPATH2 in CFLAGS and link with LIB2.

%.o: %.c
    @echo [CC]  $< ...
    $(CC) $(CFLAGS) -o $*.o $<

现在,我有一个以上的规则可以编译我的测试应用程序.现在如何根据目标更改CFLAGS.

Now I have a rule as above which compiles my test application. Now how can the CFLAGS be changed based on the target.

推荐答案

如果使用的是GNU Make,则可以使用

If you are using GNU Make, you can use target-specific variables:

target1: CFLAGS = -IINCLUDEPATH1
target1: LDLIBS = -lLIB1

target2: CFLAGS = -IINCLUDEPATH2
target2: LDLIBS = -lLIB2

all: target1 target2

target1: target1.o misc.o
target2: target2.o

但是,这并没有达到您想要的效果:如果 target1 target2 共享一些源文件,则需要安排它们每个文件都会被编译两次,并编译成不同名称的.o文件,这会使您的makefile变得更加复杂.

However this doesn't work quite as well as you'd like: if target1 and target2 share some source files, you'll need to arrange for them to each be compiled twice and to differently-named .o files -- which will rather complexify your makefile.

此外,如果键入make target1,则-IINCLUDEPATH1将根据需要传播到 misc.c 的编译中.但是,如果您键入make misc.o,则无法知道它最终是目的地为 target1 ,而 misc.c 的编译将不会获得特殊的$ CFLAGS值(尽管如果有一个,它将得到全局的一个).

Also, if you type make target1 then -IINCLUDEPATH1 will be propagated to the compilation of misc.c, as desired. However if you type make misc.o it has no way to know that this is eventually destined for target1 and the compilation of misc.c will get no special $CFLAGS value (though it'll get the global one, if there is one).

因此,这仅在简单情况下才有用.但是也许您的情况很简单.

So this is really only useful in simple cases. But maybe your case is sufficiently simple.

这篇关于根据目标更改Makefile变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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