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). 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. 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.
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: e.g.
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. 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. I want to know why i cant add to OBJ.