根据目标更改 Makefile 变量值
我不熟悉 makefile,但习惯了简单的。现在,我手头有一个任务。
我需要根据给定的目标使用不同的库和不同的包含路径来编译和链接测试应用程序。如果目标是 TARGET1,则链接到 LIB1 并在编译期间包含 INCLUDEPATH1。同样,如果给定目标是 TARGET2,则使用 CFLAGS 中的 INCLUDEPATH2 进行编译并与 LIB2 链接。
%.o: %.c
@echo [CC] $< ...
$(CC) $(CFLAGS) -o $*.o $<
现在我有一个如上所述的规则来编译我的测试应用程序。现在如何根据目标更改 CFLAGS。
I am not proficient with makefiles, but am used to simple ones. Right now, I have a task on hand.
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] lt; ...
$(CC) $(CFLAGS) -o $*.o lt;
Now I have a rule as above which compiles my test application. Now how can the CFLAGS be changed based on the target.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 GNU Make,则可以使用特定于目标的变量:
但是,这并不像您希望的那样工作:如果 target1 和 target2 共享一些源文件,您需要将它们安排到每个被编译两次并生成不同名称的 .o 文件——这会让你的 makefile 变得更加复杂。
此外,如果您输入
make target1
,则-IINCLUDEPATH1
将根据需要传播到 misc.c 的编译。但是,如果您输入makemisc.o
,它无法知道这最终是针对 target1 的,并且 misc.c 的编译将没有获得特殊的 $CFLAGS 值(尽管它会获得全局值,如果有的话)。所以这实际上只在简单的情况下有用。但也许你的情况足够简单。
If you are using GNU Make, you can use target-specific variables:
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.
Also, if you type
make target1
then-IINCLUDEPATH1
will be propagated to the compilation of misc.c, as desired. However if you typemake 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.
我不认为你可以根据目标改变变量。假设您调用
CFLAGS
那么会有什么值?在这种情况下,您可以使用非模式规则来区分目标。
为了减少重复,您还可以使用变量和“函数”。然后,您可以在不同的规则中重复使用模式规则的主体。
这将构成一个足够漂亮且灵活的 makefile 来满足您的需求。
I don't think you can alter variable depending on a target. Assume you invoke
What value would the
CFLAGS
have then?In this case, you can use non-pattern rules to distinguish targets.
To decrease repetition, you may also use variables and "functions". Then, you could re-use what would be the body of your pattern rule in different rules.
That would make a nice enough and flexible makefile that suits your needs.