Skip to content

Makefile

Jan Bartoš edited this page Mar 22, 2025 · 1 revision

Makefile Documentation

This Makefile is designed to build a C++ project, likely a terminal-based application. It provides a set of commands to compile, clean, run, and generate documentation for the project. Project Structure

The Makefile assumes the following project structure:

├── include/ # Header files (.h) ├── src/ # Source files (.cpp) ├── bin/ # Compiled object files (.o) and executable ├── docs/ # Documentation (generated by Doxygen) ├── Doxyfile # Doxygen configuration file └── Makefile # Build configuration

Prerequisites

GNU Make: The build system.

C++ Compiler (g++): To compile the C++ source code.

Doxygen: (Optional) For generating documentation.

Configuration Variables

The Makefile uses the following variables for customization:

CXX:  The C++ compiler to use (default: g++).

CXXFLAGS:  Compiler flags (default: -std=c++23 -fopenmp -Wall -Wextra -Wpedantic -Wshadow -Wsign-conversion -Werror -MMD -MP -fstack-protector-strong -D_FORTIFY_SOURCE=2 -fPIC).

SRCDIR:  The directory containing source files (default: src).

INCDIR:  The directory containing header files (default: include).

BINDIR:  The directory for output files (default: bin).

DOCDIR: The directory for documentation files (default: docs).

SRCS:  A list of source files (automatically generated).

OBJS:  A list of object files (automatically generated, located in $(BINDIR)).

TARGET: The name of the final executable (default: main.out).

Compiler Flags (CXXFLAGS)

The default CXXFLAGS include a comprehensive set of options:

-std=c++23:  Use the C++23 standard.

-fopenmp: Enable OpenMP for parallel processing.

-Wall, -Wextra, -Wpedantic:  Enable a wide range of compiler warnings.

-Wshadow: Warn about shadowing of variables.

-Wsign-conversion: Warn about implicit sign conversions.

-Werror: Treat warnings as errors.

-MMD, -MP:  Generate dependency files for automatic rebuilding.

-fstack-protector-strong:  Enable stack protection.

-D_FORTIFY_SOURCE=2:  Enable additional security checks.

-fPIC:  Generate position-independent code (useful for shared libraries).

The debug target adds -g -O0 -fsanitize=address,undefined to these flags for debugging support:

-g:  Include debugging symbols.

-O0:  Disable optimization.

-fsanitize=address,undefined: Enable address and undefined behavior sanitizers.

The release target adds -O2 -march=native to the default for optimized builds:

-O2:  Moderate optimization.

-march=native:  Optimize for the host processor.

Targets

The Makefile defines the following targets:

compile:  Builds the executable.  This is the main target.

clean:  Removes all generated files (object files, executable, and documentation).

run:  Executes the compiled program.

release: Builds an optimized executable (with -O2 -march=native).

debug: Builds a debuggable executable (with -g -O0 -fsanitize=address,undefined).

docs:  Generates project documentation using Doxygen.

How to Use

Clone the repository: Clone the repository containing the Makefile and source code.

Navigate to the project directory: cd into the project's root directory.

Build the project: Run make compile or simply make.

Run the executable: Run make run.

Clean the project: Run make clean.

Build an optimized version: Run make release.

Build a debug version: Run make debug.

Generate documentation: Run make docs.  The documentation will be available at docs/html/index.html.

Dependencies

The Makefile uses automatic dependency tracking. When you modify a source file, only the necessary object file will be recompiled. Customization

You can customize the build process by modifying the variables at the beginning of the Makefile. For example, to use a different compiler, change the CXX variable:

CXX = clang++

To add more compiler flags, append to the CXXFLAGS variable:

CXXFLAGS += -Wall -Wextra -pedantic

Clone this wiki locally