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


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
    @mkdir -p $(build_dir) $(deps_dir)

.PHONY: clean
    rm -rf $(target) $(build_dir) $(deps_dir)
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 12, 2020 at 21:19
  • \$\begingroup\$ 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. \$\endgroup\$
    – user225978
    Jun 12, 2020 at 22:05
  • \$\begingroup\$ @FooBar You have a phony PHONY target (change .PHONEY to .PHONY) \$\endgroup\$ Jun 12, 2020 at 22:29
  • \$\begingroup\$ Whoops, let me fix fix that typo! \$\endgroup\$
    – user225978
    Jun 12, 2020 at 22:30
  • \$\begingroup\$ @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. \$\endgroup\$ Jun 12, 2020 at 22:41

1 Answer 1


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).

This allows other users to easily change the compiler used when using a different environment than you.

You might also want to remove all the leading @ in favour of make -s

  • \$\begingroup\$ 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! \$\endgroup\$
    – user225978
    Jun 12, 2020 at 23:11
  • \$\begingroup\$ @FooBar I would suggest waiting a bit longer before accepting an answer. Other users may have more to add in addition to what I have written. \$\endgroup\$ Jun 12, 2020 at 23:20
  • \$\begingroup\$ Good advice @FromTheStackAndBack, but do I revoke the check mark? I would like to give you an up vote, yet the site tells me that I do not have enough reputation points to do so. \$\endgroup\$
    – user225978
    Jun 12, 2020 at 23:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.