I must have re-written it from scratch: Makefile for a tiny C++ project, follow-up 1 Thanks for any further advice in advance!
This is an additional question to C++ code reading from a text file, storing value in int, and outputting properly rounded float, not a follow-up, since I ask to review my C++ Makefile, not my C++ source code.
I did not use many Makefiles over the time. Please excuse my inexperience. All recommendations will be upvoted and welcome. Thank you in advance.
PS: It should now be more or less portable across architectures, tested on Ubuntu Noble amd64 and Debian Bookworm arm64. I don't know if I have done a good job in this area, though. Also, tested with the BSD make, not only with the GNU make with plausible results.
# Use the latest version of the compiler on Debian 12 OS, arm64 platform:
CXX=g++-12
# Use the official standard;
# Optimize for speed;
# Stop on all warnings;
# Do not include debug symbols:
CXXFLAGS=-std=c++17 -O3 -Wall -Wextra -Werror -Wpedantic -pedantic-errors
# The name of the resulting executable file:
APP_NAME=fan-control
# The name of the C++ source code file:
SRC_FILE=$(APP_NAME).cpp
# Possibly not doing anything when using GNU make;
# You may see this for an explanation:
# https://www.gnu.org/software/make/manual/html_node/Special-Targets.html#index-POSIX_002dconforming-mode_002c-setting
.POSIX:
# Force rebuild work-around (--always-make)!
# You may see these for an explanation:
# https://stackoverflow.com/a/70199176/1997354
# https://stackoverflow.com/a/79300019/1997354
# (Shell alias make='make -B' has same effect.)
.force_rebuild:
.PHONY: .force_rebuild
.PHONY: distrib
.PHONY: run
.PHONY: clean
.PHONY: all
$(APP_NAME): .force_rebuild
$(APP_NAME): $(SRC_FILE)
@printf '%s' 'Compiling program... '
@$(CXX) $(CXXFLAGS) $(SRC_FILE) -o $(APP_NAME)
@strip -s $(APP_NAME)
@printf '%s\n' 'Done.'
distrib: $(SRC_FILE) Makefile
@printf '%s' 'Compresssing source code... '
@tar --posix -czf $(APP_NAME).tar.gz $(SRC_FILE) Makefile
@printf '%s\n' 'Done.'
run: $(APP_NAME)
@printf '%s\n' 'Running program... '
@./$(APP_NAME)
clean:
@rm -f -v $(APP_NAME)
@rm -f -v $(APP_NAME).tar.gz
all: distrib run
debugtest. The debug builds with-gto create a version with debug symbols. The test target builds with coverage enabled, then builds the unit tests (in the test directory), runs the tests to make sure that all work, then checks code coverage. \$\endgroup\$installthat copies anything needed to/usr/binor/usr/libetc. But you need to be root to run the install. \$\endgroup\$