MakeItSimple - Makefiles for C++ Projects

A sample of a shell-output This github repository MakeItSimple has a set of simple to use makefiles for for small and medium sized C++ projects without the need of any further build system. These scripts build executable targets from c++source files in the current project directory and create a JSON Compilation Database for the clang language server. They only use functions that are available in a standard GNU Linux installation and do not require any additional build tools to be installed. They are therefore ideally suited to make C++ exercises without the overhead of complicated build systems.

Variants:

The variant `ProjectOutPlaceBuild2` allows the linkage of C and assembler modules along with C++ source files to the target executable.

Make automatically determines which pieces of a large program need to be recompiled, and issues commands to recompile them. For maximum performance, all make scripts support parallel build.

The scripts keeps track of the last used configuration and perform a complete build, if changes in the build configuration have been detected.

The compilation database is stored in file `compile_commands.json` in the main project directory and is automatically updated when a configuration change has been detected.

The scripts come with a comprehensive set of warning compiler options for the GNU C++ compiler and clang. These options can be controlled in 4 levels.

All make files support 2 build modes run and debug. In build mode run an optimized executable without debug information is built. In build mode debug the executable contains debug information.

All compiler options are valid for GNU C++ compiler and clang, if you use an alternative compiler adapt the options and warning flags accordingly.

If you have any suggestions or bug reports please write a Github Issue or join the discussion.

Project Out Place Build

An excerpt from the core area of the make script The Makefile builds one executable from all %.cpp and %.cc source files in all project source directories and creates a JSON Compilation Database for the clang language server. The script supports 2 Build Modes (run and debug) and 4 Warning levels. The script keeps track of the last used configuration. During startup, the script checks the current build configuration. If a change in the build configuration has been detected, a complete build is initiated. The name of the executable is obtained from the last path component of the project directory.

More features:

Configuration

Use the optional file project.mk to change the defaults:

Default warning options are overwritten with file makefile.warn:

The variables BUILD_MODE and WARN_LEVEL control the build mode and the warning level.
More compiler options can be given during invocation with variables CPPFLAGS, CXXFLAGS, LDFLAGS, LDLIBS and TARGET_ARCH.

Execute the help goal to read the Makefile help.

make help

Source Detection

The cpp source files are detected in the configured list source directories SRCDIRS. This is implemented with make function wildcard. This works not recursive. If you want a recursive search, you must use the following line

CPPSOURCES := $(shell find $(SRCDIRS) -name '*.cpp')

Automatic Generation of Include Directory Flags

The include flags for the include directories are generated automatically from the configured variable INCDIRS and INCSYSDIRS. You may use the following code to detect the list of the project internal include directories with:

INCDIRS := $(shell find $(SRCDIRS) -type d)

Project Out Place Build 2

An excerpt from the core area of the make script The Makefile has basically the same functionality as the variant ProjectOutPlaceBuild but links additionally C sources and assembler source file into the final executable.

Project In Place Build

An excerpt from the core area of the make script The Makefile builds one executable from all %.cpp and %.cc source files in the current/project directory and creates a JSON Compilation Database for the clang language server. The name of the executable is obtained from the last path component of the project directory.

More features:

Configuration

Project settings are stored in a separate file project.mk:

Default warning options are overwritten with file makefile.warn:

The variables BUILD_MODE and WARN_LEVEL control the build mode and the warning level. More compiler options can be given during invocation with variables INCSYSDIRS, CPPFLAGS, CXXFLAGS, LDFLAGS, LDLIBS and TARGET_ARCH.

Execute the help goal to read the Makefile help.

make help

One To One

An excerpt from the core area of the make script The Makefile builds executable targets from each %.cpp and %.cc source file found in the current directory and creates a JSON Compilation Database for the clang language server.

More features:

Configuration

Default warning options are overwritten with file makefile.warn:

The variables BUILD_MODE and WARN_LEVEL control the build mode and the warning level. More compiler options can be given during invocation with variables INCSYSDIRS, CPPFLAGS, CXXFLAGS, LDFLAGS, LDLIBS and TARGET_ARCH.

Execute the help goal to read the Makefile help.

make help

Make Goals

All Makefiles support the following goals:

Other Resources

GNU Make
GNU Make Manual
Learn makefiles by example
Auto Dependency Generation
Delete On Error
A Simple C++ Makefile Example
A Super-Simple Makefile