# Optimization of General Purpose Makefile

I wrote a makefile that would generate a simple executable from the example directory structure.

project/
source/
include/
makefile


The makefile itself comes equipped with dependency tracking, and automatically generates two directories named build and dependency. Currently the makefile I made works, yet it takes anywhere from three to five seconds to build the executable on my machine. I was wondering whether my makefile source code was setup inefficiently, or am I just being impatient?

target    := driver.exe
compiler  := g++
ccflags   := -Wall -Werror -pedantic -pedantic-errors -O2 -std=c++17
src_dir   := source
build_dir := build
deps_dir  := dependency
ccfiles   := $(wildcard$(src_dir)/*.cc)
objects   := $(patsubst$(src_dir)/%.cc, $(build_dir)/%.o,$(ccfiles))

deps := $(patsubst$(build_dir)/%.o, $(deps_dir)/%.d,$(objects))
-include $(deps) deps_flags = -MMD -MF$(@:$(build_dir)/%.o=$(deps_dir)/%.d)

$(target):$(objects)
@$(compiler) -o$@ $^ @echo "Successfully built "$@"!"

$(build_dir)/%.o:$(src_dir)/%.cc | setup
@$(compiler)$(cflags) -o $@ -c$< $(deps_flags) @echo "Successfully built "$<" into "$@"!" .PHONY: setup setup: @mkdir -p$(build_dir) $(deps_dir) .PHONY: clean clean: rm -rf$(target) $(build_dir)$(deps_dir)

• Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. – Sᴀᴍ Onᴇᴌᴀ Jun 12 '20 at 21:19
• There we go! Hopefully, the edits I made now narrow down what I would like advice on! I really hope that the makefile I created is inefficient. Otherwise, this makefile just proves how old my computer is. – Snuggly Plushy Jun 12 '20 at 22:05
• @FooBar You have a phony PHONY target (change .PHONEY to .PHONY) – FromTheStackAndBack Jun 12 '20 at 22:29
• Whoops, let me fix fix that typo! – Snuggly Plushy Jun 12 '20 at 22:30
• @FooBar "I was wondering whether my makefile source code was setup inefficiently": I doubt that this is because of your Makefile. Of course, one way to test it would be to use time g++ -Wall -Werror -pedantic -pedantic-errors -O2 -std=c++17 -c ... (that is, compile the object files and link them without using Make's pattern subsitution). You should make sure to do these two steps (compile and link) separately to account for possibly writing the .o files to disk. – FromTheStackAndBack Jun 12 '20 at 22:41

Instead of defining your own compiler variable, I would override the implicit CXX variable. This also goes for the implicit CXXFLAGS (instead of your ccflags).
You might also want to remove all the leading @ in favour of make -s
• I see your point @FromTheStackAndBack. I also did the time test you suggested, and you were right! Turns out a I left a few applications running in the background. After shutting them down, my makefile builds my executable quickly. Now I don't know if this question was a waste, but I did learn some new things about make, thank you! – Snuggly Plushy Jun 12 '20 at 23:11