This repository provides two complete build system configurations for C++ projects:
- Makefile - Traditional GNU Make setup (Linux/MacOS-focused)
- CMakeLists.txt - Modern cross-platform CMake configuration
- Build System Comparison
- Prerequisites
- Makefile Usage (Linux/MacOS)
- CMake Usage (Windows/Linux/MacOS)
- Project Structure
- Choosing a Build System
| Feature | Makefile | CMake |
|---|---|---|
| Primary OS | Linux/MacOS | Windows/Linux/MacOS |
| Compiler Support | GCC/Clang | MSVC/GCC/Clang |
| Build Modes | Debug/Release (via mode=) |
Debug/Release (native) |
| Dependency Tracking | Manual | Automatic |
| IDE Integration | Basic | Excellent (VS, CLion, etc.) |
| Header Handling | Implicit | Explicit tracking |
- GNU Make 4.1+
- GCC 11+ or Clang 14+
- Linux/MacOS or Windows WSL
- CMake 3.15+
- Any C++20 compiler:
- MSVC 2022 (Windows)
- MinGW-w64 8.1+ (Windows)
- GCC 11+ (Linux)
- Clang 14+ (MacOS)
- Strict C++23 compliance checks
- Automatic source discovery
- Debug/Release mode switching
- Clean build artifacts
To use this Makefile:
# Build debug version (default)
make
# Build and run:
make run
# Build debug version
make mode=debug
# Build release version
make mode=release
# Clean build artifacts
make clean
# Custom target name
make TARGET=bin/my_app# Clone project
git clone https://github.com/your/project.git
cd project
# Build debug version
make
# Run program
./bin/my_program
# Clean build
make clean- Cross-platform support
- IDE project generation
- Compiler-agnostic configuration
- Detailed build reports
- Header file integration
- Multi-configuration support
-
Install dependencies:
- [Visual Studio 2022] with C++ tools
- [CMake] 3.15+
- [Ninja] or [MinGW] (optional)
-
Open terminal in project directory:
# Generate Visual Studio solution
cmake -S . -B build -G "Visual Studio 17 2022"
# Or for MinGW
cmake -S . -B build -G "MinGW Makefiles"# Configure project (general)
cmake -S . -B build [-G "Generator Name"]
# Build project
cmake --build build [--config Release]
# Run executable (Windows)
.\bin\main.exe
# Run executable (Linux/MacOS)
./bin/mainCommon Generators:
| Platform | Generator Command |
|---|---|
| Windows | -G "Visual Studio 17 2022" |
-G "MinGW Makefiles" |
|
| Linux | -G "Unix Makefiles" |
| MacOS | -G "Xcode" |
# Configure with MinGW
cmake -S . -B build -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release
# Build release version
cmake --build buildThis project follows a build process where:
- Intermediate build files are generated in a separate
build/directory (standard CMake practice). - For convenience during development, the final compiled executable is placed in a
bin/directory located directly within the project's source code tree.
Because of step 2, the standard CMake clean command (cmake --build . --target clean, make clean, or ninja clean) is not sufficient for a complete cleanup. It will remove files inside the build/ directory, but it will not touch the bin/ directory in your source tree.
To provide a simple, one-step method for removing all build-related output (both the build/ directory and the bin/ directory), use the following scripts located in the project root:
-
For Linux, macOS, or Git Bash on Windows:
-
clean.sh -
Usage:
-
First, make the script executable (you only need to do this once):
chmod +x clean.sh
-
Run the script from the project's root directory:
./clean.sh
-
-
-
For Windows Command Prompt or PowerShell:
-
clean.bat -
Usage:
-
Run the script from the project's root directory:
.\clean.bat
-
-
Running the appropriate script will permanently delete both the build/ and bin/ directories and their contents, allowing you to start a completely fresh build.
Both build systems expect the following project structure:
project-root/
├── src/ # Source files (.c, .cpp, .cc, .cxx)
│ ├── main.cpp
│ └── ...
├── bin/ # Generated executables
├── obj/ # Object files (Makefile only)
├── Makefile # Linux build config
└── CMakeLists.txt # Cross-platform config
-
Use Makefile if:
- Developing exclusively on Linux/MacOS
- Need minimal setup
- Prefer direct compiler control
- Working with legacy systems
-
Use CMake if:
- Developing on Windows
- Need IDE integration
- Require cross-platform builds
- Want future-proof configuration
- Managing complex projects
Both systems enforce:
- Strict C++20 compliance
- Warnings-as-errors policy
- Clean separation of source/build artifacts
- Multiple compiler support
Note: The CMake configuration is recommended for new projects due to its cross-platform capabilities and modern features.