2
\$\begingroup\$

It's been pointed out (see the first comment) that my makefile rebuilds all source files regardless of changes made.

# Variables #

TARGETS := libAurora.a libAurora.so
    # The names of targets that can be built.
    # This is used in the list of valid targets when no target is specified, and when building all targets.

TARGET_DIRECTORY := ./Binaries
    # The place to put the finished binaries.

CXX := g++
    # The compiler to use to compile the source code into object files.

CXXFLAGS := -I. -Wall -Wextra -fPIC -g -O4
    # Options passed to the compiler when compiling source files into object files.

AR := ar
    # The archiver to use to consolidate the object files into one library.

ARFLAGS := -rcs
    # Options to be passed to the archiver.

SOURCE_FILES := $(shell find Source -type f -name *.cpp)
    # Because it's inconvenient to maintain an up-to-date list of all the source files
    # that should be compiled, we just search for all .cpp files in the project

OBJECT_FILES := $(SOURCE_FILES:.cpp=.o)

.PHONY: Default
    # The default target, which doesn't actually do anything except give the user instructions,
    # can be called even if nothing needs to be updated.

.INTERMEDIATE: %.o
    # Specifying the object files as intermediates deletes them automatically after the build process.

# Rules #

Default:
    @echo "Please specify a target, or use \"All\" to build all targets.  Valid targets:"
    @echo "$(TARGETS)"

All: $(TARGETS)

lib%.a: $(OBJECT_FILES)
    $(AR) $(ARFLAGS) $(TARGET_DIRECTORY)/$@ $(OBJECT_FILES)

lib%.so: $(OBJECT_FILES)
    $(AR) $(ARFLAGS) $(TARGET_DIRECTORY)/$@ $(OBJECT_FILES)

What can I do to improve it?

\$\endgroup\$
2
\$\begingroup\$

I think the suggestion in the link you mention is to not bother with making your object files "intermediate". I tend to agree.

If make deletes the object files after the link step then it has to re-build them all at the next invocation of make. Without this, the next time you make, it can look at the dependencies of the object files (as specified by the makefile, the part after the :) and only rebuild them if the stated dependencies have a later timestamp.

You might also want a make clean target to remove the objects via rm, so that you can delete them when you really want to. But the typical workflow for using make is to keep your object files around for extended periods and type make when you want to build what you've changed.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.