I have two targets in my Makefile, namely:
all: $(TARGET)
valgrind: $(TARGET)
...
...
...
The valgrind
target simply runs the executable with the valgrind program. The CFLAGS
for this Makefile includes:
CFLAGS += -fsanitize=address
CFLAGS += -fsanitize=undefined
CFLAGS += -fsanitize=bounds-strict
CFLAGS += -fsanitize=leak
CFLAGS += -fsanitize=null
CFLAGS += -fsanitize=signed-integer-overflow
CFLAGS += -fsanitize=bool
CFLAGS += -fsanitize=pointer-overflow
CFLAGS += -fsanitize-address-use-after-scope
CFLAGS += -fanalyzer
When doing make valgrind
(assuming the target was already built), I have to first run make clean
(this simply removes *.obj/
and $(TARGET)
, and then comment out these flags because Valgrind and sanitizers do not work together. And then uncomment them back for the next build, and so on.
How can I make the valgrind
target unconditionally rebuild the target each time but with the sanitizers disabled?
The whole Makefile (with some fluff removed):
CC = gcc-13
CFLAGS += -DBENCHMARKING
CFLAGS += -O3
CFLAGS += -std=c2x
CFLAGS += -s
CFLAGS += -no-pie
CFLAGS += -fno-builtin
CFLAGS += -fno-common
CFLAGS += -fno-omit-frame-pointer
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -Warray-bounds
CFLAGS += -Wconversion
CFLAGS += -Wformat-signedness
CFLAGS += -Wno-parentheses
CFLAGS += -Wpedantic
CFLAGS += -pendatic-errors
CFLAGS += -Wstrict-prototypes
CFLAGS += -Wwrite-strings
CFLAGS += -Wno-missing-braces
CFLAGS += -Wno-missing-field-initializers
CFLAGS += -fsanitize=address
CFLAGS += -fsanitize=undefined
CFLAGS += -fsanitize=bounds-strict
CFLAGS += -fsanitize=leak
CFLAGS += -fsanitize=null
CFLAGS += -fsanitize=signed-integer-overflow
CFLAGS += -fsanitize=bool
CFLAGS += -fsanitize=pointer-overflow
CFLAGS += -fsanitize-address-use-after-scope
CFLAGS += -fanalyzer
SRC_DIR = src
SRCS = $(wildcard $(SRC_DIR)/*.c)
OBJ_DIR = obj
OBJS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRCS))
DEPS = $(OBJS:.o=.d)
TARGET = read_file
all: $(TARGET)
$(TARGET): $(OBJ_DIR) $(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS)
$(OBJ_DIR):
$(MKDIR) $(OBJ_DIR)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) -MMD -c -o $@ $<
-include $(DEPS)
valgrind: $(TARGET)
valgrind --tool=memcheck --leak-check=yes ./read_file --mmap_memchr read_file
valgrind --tool=memcheck --leak-check=yes ./read_file --getline read_file
valgrind --tool=memcheck --leak-check=yes ./read_file --mmap_getline read_file
valgrind --tool=memcheck --leak-check=yes ./read_file --fread read_file