I am currently learning c++. I wrote the below Makefile to compile my program:
NAME = test
CC = c++
CFLAGS = -Wall -Wextra -Werror -std=c++98
SRCS = test.cpp
OBJS = ${SRCS:.cpp=.o}
all: ${NAME}
%.o: ${SRCS}
${CC} ${CFLAGS} -c $< -o $@
${NAME}: ${OBJS}
${CC} ${CFLAGS} ${OBJS} -o ${NAME}
clean:
rm ${OBJS}
fclean: clean
rm ${NAME}
re: fclean
all
.PHONY: all clean fclean re .c.o
The Makefile works fine until I use the touch test.cpp
command to see if it relinks as I intended to.
My Makefile then first recompiles test.cpp
as it should, then it tries to link Makefile.o
and output a file Makefile
.
Below is the output display on my terminal:
c++ -Wall -Wextra -Werror -std=c++98 -c test.cpp -o Makefile.o
c++ Makefile.o -o Makefile
rm Makefile.o
Makefile:1: warning: NUL character seen; rest of line ignored
Makefile:1: *** missing separator. Stop.
On top of that, my entire Makefile gets replaced by random strings and characters (not really random but I have no idea where are they from, and a whole bunch of NUL
characters), here is a screenshot of what it looks like:
I code under WSL2, Windows 11, Ubuntu 20.04, in VSCode.
I would really appreciate some idea, thanks!
I solved the problem by removing the %.o
rule, so I just compiled with ${CC} ${CFALGS} ${SRCS} -o ${NAME}
.
But I still want to know why compiling to object files first caused the problem