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)
PHONY
target (change.PHONEY
to.PHONY
) \$\endgroup\$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. \$\endgroup\$