根据目标更改 Makefile 变量值

发布于 2024-09-10 06:55:17 字数 341 浏览 1 评论 0原文

我不熟悉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没有 2024-09-17 06:55:17

如果您使用的是 GNU Make,则可以使用特定于目标的变量

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

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

all: target1 target2

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

但是,这并不像您希望的那样工作:如果 target1target2 共享一些源文件,您需要将它们安排到每个被编译两次并生成不同名称的 .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:

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

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

all: target1 target2

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

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 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.

南七夏 2024-09-17 06:55:17

我不认为你可以根据目标改变变量。假设您调用

make TARGET1 TARGET2

CFLAGS 那么会有什么值?

在这种情况下,您可以使用非模式规则来区分目标。

TARGET1: a.c
    @echo [CC] 
lt; ...
    $(CC) -I INCLUDEPATH1 ...

TARGET2: a.c
    @echo [CC] 
lt; ...
    $(CC) -I INCLUDEPATH2 ...

为了减少重复,您还可以使用变量和“函数”。然后,您可以在不同的规则中重复使用模式规则的主体。

define compile_cmd
    @echo [CC] 
lt; ...
    $(CC) -I $1 -l$2 $(CFLAGS)
endef

TARGET1: a.c
    $(call compile_cmd,INCLUDEPATH1,LIB1) -o $@ 
lt;

TARGET2: a.c
    $(call compile_cmd,INCLUDEPATH2,LIB2) -o $@ 
lt;

%.o: %.c
    $(call compile_cmd,INCLUDEPATH_DEFAULT,LIB_DEFAULT) -o $@ 
lt;

这将构成一个足够漂亮且灵活的 makefile 来满足您的需求。

I don't think you can alter variable depending on a target. Assume you invoke

make TARGET1 TARGET2

What value would the CFLAGS have then?

In this case, you can use non-pattern rules to distinguish targets.

TARGET1: a.c
    @echo [CC] 
lt; ...
    $(CC) -I INCLUDEPATH1 ...

TARGET2: a.c
    @echo [CC] 
lt; ...
    $(CC) -I INCLUDEPATH2 ...

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.

define compile_cmd
    @echo [CC] 
lt; ...
    $(CC) -I $1 -l$2 $(CFLAGS)
endef

TARGET1: a.c
    $(call compile_cmd,INCLUDEPATH1,LIB1) -o $@ 
lt;

TARGET2: a.c
    $(call compile_cmd,INCLUDEPATH2,LIB2) -o $@ 
lt;

%.o: %.c
    $(call compile_cmd,INCLUDEPATH_DEFAULT,LIB_DEFAULT) -o $@ 
lt;

That would make a nice enough and flexible makefile that suits your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文