Simple C++ Makefile Example

 www.partow.net  .: Home :.   .: Links :.   .: Search :.   .: Contact :. 


Description

This is a very simple C++ Makefile example and associated template, that can be used to get small to medium sized C++ projects up and running quickly and easily. The Makefile assumes source code for the project is broken up into two groups, headers (*.hpp) and implementation files (*.cpp).

The source code and directory layout for the project is comprised of three main directories (include, src and build), under which other directories containing code would reside. The layout used in the example is as follows:


─┬[ Project ]
 │
 ├──● Makefile
 │
 ├──┬[ build ]
 │  │
 │  ├───[ objects ]
 │  └───[ apps ]
 │
 ├──┬[ include ]
 │  │
 │  ├──● program.hpp
 │  │
 │  ├──┬[ module1 ]
 │  │  │
 │  │  ├──● mod1c1.hpp
 │  │  └──● mod1c2.hpp
 │  │
 │  └──┬[ module2 ]
 │     │
 │     ├──● mod2c1.hpp
 │     └──● mod2c2.hpp
 │
 └──┬[ src ]
    │
    ├──● program.cpp
    │
    ├──┬[ module1 ]
    │  │
    │  ├──● mod1c1.cpp
    │  └──● mod1c2.cpp
    │
    └──┬[ module2 ]
       │
       ├──● mod2c1.cpp
       └──● mod2c2.cpp

Directory Purpose
Project / include Header files (*.hpp, *.h, *.hxx, *.h++)
Project / src Implementation files (*.cpp)
Project / build / objects Object files (*.o)
Project / build / apps Executables



The Makefile

The Makefile supports building of a single target application called program which once built will be placed in the build/apps directory. All associated objects will be placed in the build/objects directory. The following is a listing of the Makefile in its entirety:

CXX      := -c++
CXXFLAGS := -pedantic-errors -Wall -Wextra -Werror
LDFLAGS  := -L/usr/lib -lstdc++ -lm
BUILD    := ./build
OBJ_DIR  := $(BUILD)/objects
APP_DIR  := $(BUILD)/apps
TARGET   := program
INCLUDE  := -Iinclude/
SRC      :=                      \
   $(wildcard src/module1/*.cpp) \
   $(wildcard src/module2/*.cpp) \
   $(wildcard src/*.cpp)         \

OBJECTS  := $(SRC:%.cpp=$(OBJ_DIR)/%.o)
DEPENDENCIES \
         := $(OBJECTS:.o=.d)

all: build $(APP_DIR)/$(TARGET)

$(OBJ_DIR)/%.o: %.cpp
	@mkdir -p $(@D)
	$(CXX) $(CXXFLAGS) $(INCLUDE) -c $< -MMD -o $@

$(APP_DIR)/$(TARGET): $(OBJECTS)
	@mkdir -p $(@D)
	$(CXX) $(CXXFLAGS) -o $(APP_DIR)/$(TARGET) $^ $(LDFLAGS)

-include $(DEPENDENCIES)

.PHONY: all build clean debug release info

build:
	@mkdir -p $(APP_DIR)
	@mkdir -p $(OBJ_DIR)

debug: CXXFLAGS += -DDEBUG -g
debug: all

release: CXXFLAGS += -O2
release: all

clean:
	-@rm -rvf $(OBJ_DIR)/*
	-@rm -rvf $(APP_DIR)/*

info:
	@echo "[*] Application dir: ${APP_DIR}     "
	@echo "[*] Object dir:      ${OBJ_DIR}     "
	@echo "[*] Sources:         ${SRC}         "
	@echo "[*] Objects:         ${OBJECTS}     "
	@echo "[*] Dependencies:    ${DEPENDENCIES}"
                       

The Makefile and a complete example including source code and directory layout can be downloaded from: HERE



Makefile Commands

The following commands can be used with this Makefile:

  • make all
  • make clean
  • make program
  • make build
  • make release
  • make debug
  • make info


Example Run

The following is the expected output when the command "make clean all" is executed:

make clean all
rm -rf build/*
c++ -pedantic-errors -Wall -Wextra -Werror -Iinclude/ -o build/objects/src/program.o
   -c src/program.cpp
c++ -pedantic-errors -Wall -Wextra -Werror -Iinclude/ -o build/objects/src/module1/mod1c1.o
   -c src/module1/mod1c1.cpp
c++ -pedantic-errors -Wall -Wextra -Werror -Iinclude/ -o build/objects/src/module1/mod1c2.o
   -c src/module1/mod1c2.cpp
c++ -pedantic-errors -Wall -Wextra -Werror -Iinclude/ -o build/objects/src/module2/mod2c1.o
   -c src/module2/mod2c1.cpp
c++ -pedantic-errors -Wall -Wextra -Werror -Iinclude/ -o build/objects/src/module2/mod2c2.o
   -c src/module2/mod2c2.cpp
c++ -pedantic-errors -Wall -Wextra -Werror -Iinclude/ -L/usr/lib -lstdc++ -lm
-o build/apps/program
build/objects/src/program.o
build/objects/src/module1/mod1c1.o
build/objects/src/module1/mod1c2.o
build/objects/src/module2/mod2c1.o
build/objects/src/module2/mod2c2.o


CMake Version

A CMake based build configuration of the above mentioned project structure can be found HERE



© Arash Partow. All Rights Reserved.