Romain-p Romain-p - 1 month ago 9
C Question

Remove temporary files after compiling with Makefile

I'm currently using the following Makefile to compile my C project.

RM = rm -f
CFLAGS += -Wextra -Wall -Werror
CFLAGS += -I.

NAME = a.out

SRCS = main.c

OBJS = $(SRCS:.c=.o)


all: $(NAME)
$(NAME): $(OBJS)
gcc $(OBJS) -o $(NAME) $(LDFLAGS)

clean:
$(RM) $(OBJS)

fclean: clean
$(RM) $(NAME)

re: fclean all


I would like to add something into my clean command which could remove temporary files, that's to say the files ending with
~
.
I have to do this without use
*~
.

Answer
TMP =  $(SRCS:.c=.c~)

clean:
        $(RM) -f $(OBJS)
        $(RM) -f $(TMP)

or like @Barmar said

clean:
    -$(RM) $(OBJS)
    -$(RM) $(TMP)

it's better to avoid option in RM = rm -f ==> RM = rm

doc