This is a completely refactored code of Makefile for a tiny C++ project based on all of the information in answers there, also comments, and a bit of my common sense.
I did my best, I worked really hard on it, read twice every comment, every bit of the answers, and also I read some of the documentation.
Now, I appear to reach the end of the road, without further response from you, that is. I have re-written it from scratch.
I forgot to copy from the original question, that the solution must be portable, e.g. $(PROGRAM).o: CXXFLAGS += -g is not portable, I tried it with BSD make, it errored out, unfortunatelly. So, in essence, I forgot to include also in here, that the Makefile must be portable = BSD make usable. Thank you, and late apology.
If you do not explicitly want, you do not need to read the original question at all. If you haven't already, that is.
# Copyright 2024 Vlastimil Burian under MIT license
# This is the project's Makefile version 0.1 alpha!
# Bug reports email: [email protected]
CXX = g++
CXXFLAGS = -std=c++11 -O3 -Wall -Wextra -Werror -Wpedantic -pedantic-errors
PROGRAM = fan-control
.PHONY: all debug run distrib clean install uninstall
all: $(PROGRAM)
$(PROGRAM).o: $(PROGRAM).cpp
$(CXX) -c -g $(CXXFLAGS) $(PROGRAM).cpp -o $(PROGRAM).o
$(PROGRAM): $(PROGRAM).o
$(CXX) $(CXXFLAGS) $(PROGRAM).o -o $(PROGRAM)
strip -s $(PROGRAM)
debug: $(PROGRAM).o
$(CXX) -g $(CXXFLAGS) $(PROGRAM).o -o $(PROGRAM)
run: $(PROGRAM)
./$(PROGRAM)
distrib: $(PROGRAM).cpp Makefile
tar -H posix -czf $(PROGRAM).tar.gz $(PROGRAM).cpp Makefile
clean:
rm -fv *.o $(PROGRAM) $(PROGRAM).tar.gz
install:
@if [ ! -f $(PROGRAM) ]; then printf '%s\n' 'You need to run either `make`, or `make debug` first.' && exit 1; fi
@if [ ! `id -u` -eq 0 ]; then printf '%s\n' 'You need to run `install` target either with `sudo`, or as root.' && exit 1; fi
@if [ ! -d /usr/local/bin ]; then printf '%s\n' 'This program wanted to install into /usr/local/bin, but that directory does not exist on this system.' && exit 1; fi
cp -v -i $(PROGRAM) /usr/local/bin/
uninstall:
@if [ ! -f /usr/local/bin/$(PROGRAM) ]; then printf '%s\n' '/usr/local/bin/$(PROGRAM) is not installed.' && exit 1; fi
@if [ ! `id -u` -eq 0 ]; then printf '%s\n' 'You need to run `uninstall` target either with `sudo`, or as root.' && exit 1; fi
rm -v /usr/local/bin/$(PROGRAM)