简体   繁体   English

为什么我不能添加到 OBJ,但我可以添加到 MAKEFILE 中的 CFLAGS/SRC

[英]Why can't i add to the OBJ's but i can add to the CFLAGS/SRC's in a MAKEFILE

I want two Makefile targets which both create the same targetfile but with some bonus files added to the normal files in the bonus rule (all files inside the final.a though).我想要两个 Makefile 目标,它们都创建相同的目标文件,但在奖励规则中将一些奖励文件添加到普通文件中(尽管所有文件都在 final.a 中)。 Which in itself is pretty easy, but i want both rules to not relink.这本身很简单,但我希望这两个规则都不要重新链接。 By not relinking i mean not executing the ar command if the prereq-files didn't change.不重新链接我的意思是如果先决条件文件没有改变则不执行 ar 命令。 So showing that "Nothing to be done for target" in the terminal is what i want.因此,在终端中显示“无需为目标做任何事情”就是我想要的。

I thought about changing the OBJ'S var before calling the same $(NAME) target to get that to happen.我考虑过在调用相同的 $(NAME) 目标之前更改 OBJ 的 var 来实现它。

SRC = test1.c
BSRC = test2.c

OBJ = $(SRC:.c=.o) 
BOBJ = $(BSRC:.c=.o)
NAME = libtest.a
CC = gcc

all: $(NAME)

bonus: OBJ += $(BOBJ)
bonus: $(NAME)

$(NAME): $(OBJ)
    ar rcs $@ $^

this will result in:这将导致:

compile_test> make bonus 
gcc    -c -o test1.o test1.c
ar rcs libtest.a test1.o

i am in confusion about why the first line of the bonus rule isn't working.我对为什么奖金规则的第一行不起作用感到困惑。 I can add to the CFLAGS or to the SRC's: eg我可以添加到 CFLAGS 或 SRC 中:例如

SRC = test1.c
BSRC = test2.c

OBJ = $(SRC:.c=.o) 
BOBJ = $(BSRC:.c=.o)
NAME = libtest.a
CC = gcc
CFLAGS = -Wall

all: $(NAME)

bonus: SRC += $(BSRC)
bonus: OBJ += $(BOBJ)
bonus: CFLAGS += -g
bonus: $(NAME)

$(NAME): $(OBJ)
    ar rcs $@ $^

$(OBJ): $(SRC)
    $(CC) $(SRC) $(CFLAGS) -c

will run like this:将像这样运行:

compile_test> make bonus 
gcc test1.c test2.c -Wall -g -c
ar rcs libtest.a test1.o

so it added to the SRC and to the CFLAG but not to the OBJ.所以它添加到 SRC 和 CFLAG 但没有添加到 OBJ。 At first i thought it would be something with $(OBJ) beeing a prerequisit of the target, but then after this test adding to SCR (a prerequisite as well) that idea got rewoked.起初我以为 $(OBJ) 是目标的先决条件,但在这个测试添加到 SCR(也是先决条件)之后,这个想法被重新唤醒了。 I want to know why i cant add to OBJ.我想知道为什么我不能添加到 OBJ。

I think I see;我想我明白了; you mean "compiling" where you say "linking" in your question.你的意思是“编译”,你在问题中说“链接”。 I will explain what is happening by expanding the variables in your makefile.我将通过扩展 makefile 中的变量来解释发生了什么。

So, after expanding this is what make sees for rules:因此,在扩展之后,这就是 make 看到的规则:

bonus: SRC += test2.c
bonus: OBJ += test2.o
bonus: CFLAGS += -g
bonus: libtest.a

libtest.a: test1.o                    # 1
        ar rcs libtest.a test1.o      # 2

test1.o: test1.c                      # 3
        gcc test1.c test2.c -Wall -c  # 4

At line #1 we can see that $(OBJS) expanded to test1.o , because as described in the docs the target-specific change only applies to recipes, not prerequisites.在第 1 行,我们可以看到$(OBJS)扩展为test1.o ,因为如文档中所述,特定于目标的更改仅适用于配方,不适用于先决条件。

At line #2 we can see that $^ expands to just test1.o because that's the list of prerequisites for this rule.在第 2 行,我们可以看到$^扩展为test1.o ,因为这是该规则的先决条件列表。

At line #3 we can see that $(OBJS) expands to test1.o and $(SRC) expands to test1.c for the same reason as above: target-specific variables are in effect only in recipes not in targets or prerequisites.在第 3 行,我们可以看到$(OBJS)扩展为test1.o$(SRC)扩展为test1.c ,原因与上述相同:特定于目标的变量仅在配方中有效,而不在目标或先决条件中有效。

At line #4 we can see that $(SRCS) expands to both test1.c and test2.c , because finally here we're using a target-specific variable setting inside a recipe.在第 4 行,我们可以看到$(SRCS)扩展为test1.ctest2.c ,因为最后在这里我们在配方中使用特定于目标的变量设置。 But, this rule is wrong because (a) it builds two output files: test1.o and test2.o , but (b) the target test1.o in the rule tells make that this rule only builds test1.o .但是,这个规则是错误的,因为 (a) 它构建了两个 output 文件: test1.otest2.o ,但是 (b) 规则中的目标test1.o告诉 make 这个规则只构建test1.o

This is the reason make runs the compiler twice if you run make bonus : it tries to build test1.o using your recipe, but it doesn't know that this rule builds two output files so it uses a built-in recipe to also build test2.o .这就是 make 在运行make bonus时运行编译器两次的原因:它尝试使用您的配方构建test1.o ,但它不知道此规则构建了两个 output 文件,因此它使用内置配方来构建test2.o

Before we can tell you what you need to change, you need to tell us what you actually want your makefile to do when you run make bonus because it's really not clear.在我们告诉您需要更改什么之前,您需要告诉我们您在运行make bonus时实际希望您的 makefile 做什么,因为它真的不清楚。 Do you want it to build the "normal" libtest.a , which contains only test1.o , and then in addition build an extra test2.o which is not in libtest.a ?您是否希望它构建仅包含test1.o的“正常” libtest.a ,然后另外构建一个不在libtest.a中的额外test2.o Or do you want libtest.a to contain both objects if you run make bonus ?或者,如果您运行make bonus ,您是否希望libtest.a包含这两个对象?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM