Romain-p Romain-p - 1 year ago 163
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

NAME = a.out

SRCS = main.c

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

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

$(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 Source
TMP =  $(SRCS:.c=.c~)

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

or like @Barmar said

    -$(RM) $(OBJS)
    -$(RM) $(TMP)

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download