Skip to content

Instantly share code, notes, and snippets.

@riking

riking/Makefile Secret

Created September 21, 2016 22:35
Show Gist options
  • Save riking/9a1dff3f1c1b36e6dbfce53e52a325ff to your computer and use it in GitHub Desktop.
Save riking/9a1dff3f1c1b36e6dbfce53e52a325ff to your computer and use it in GitHub Desktop.
Makefile making mysterious rm command
FILENAMES += ft_isalpha.c ft_isdigit.c ft_isalnum.c ft_isascii.c ft_isprint.c
FILENAMES += ft_toupper.c ft_tolower.c
TESTS += char_is
# Remark: I have a file named test_char_is.c that contains a main()
FILENAMES += # ... more ...
TESTS += # ... more ...
NAME = libft.a
CC = gcc
CFLAGS += -Wall -Wextra -Werror
LDFLAGS += -Wall -Wextra -Werror
ifeq ($(DEBUG), 1)
CFLAGS += -fsanitize=address
LDFLAGS += -fsanitize=address
else
#CFLAGS += -O3 -fomit-frame-pointer -DNDEBUG -flto
#LDFLAGS += -O3 -fomit-frame-pointer -DNDEBUG -flto
endif
SRCS = $(FILENAMES)
OBJS = $(addprefix build/, $(FILENAMES:.c=.o))
TESTTARGETS = $(addprefix test-, $(TESTS))
TESTBINS = $(addprefix build/, $(TESTTARGETS))
.PHONY: all clean fclean re test $(TESTTARGETS)
all: $(NAME)
re: fclean all
clean:
rm -rf build/
rm -f $(TESTBINS)
fclean: clean
rm -f $(NAME)
build:
mkdir build/
$(NAME): $(OBJS)
ar rcs $@ $^
build/%.o: %.c libft.h | build
$(CC) $(CFLAGS) -c $< -o $@
test: $(TESTBINS)
for bin in $(TESTBINS); do \
echo $$bin ; \
$$bin ; \
echo ; \
done
build/test-%: build/test_%.o libft.a | build
$(CC) $(LDFLAGS) -o $@ $^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment