Skip to content

Latest commit

 

History

History
156 lines (106 loc) · 6.36 KB

README.md

File metadata and controls

156 lines (106 loc) · 6.36 KB

Algorithm-Learning-Project

This repository is a modern CMake template optimized for learning, practicing, and experimenting with C/C++ algorithms and data structures. It simplifies the build configuration process by completely eliminating the need to manually update CMakeLists.txt every time you write a new exercise or code snippet.

✨ Key Features

  • Automated Source Discovery: Automatically scans and turns every exercise file under src/ch*/ into an independent executable.
  • Modular Architecture: Reusable algorithms and data structures in src/common/ are automatically compiled into a static library (alg_lib) and linked to all chapter exercises.
  • Collision-Free Target Naming: Prevents CMake target conflicts by automatically namespacing targets with their relative paths (e.g., src/ch2/b.cpp becomes target ch2_b).
  • Seamless LSP Integration: Auto-generates compile_commands.json and copies it to the project root for full out-of-the-box autocomplete and diagnostics support with Clangd.
  • Cross-Platform Presets: Out-of-the-box support for Ninja, MinGW, and MSVC through standard CMakePresets.json.

📂 Recommended Project Structure

To ensure that the dependencies and source files are correctly scanned by CMake, please organize your project files according to the following layout:

Your-Algorithm-Learning-Project/
├── CMakeLists.txt         # Core build configuration
├── CMakePresets.json      # Pre-configured build/configure environments
├── include/               # Public header files (.h / .hpp)
│   └── custom_data_structure.h
└── src/                   # Source files organized by common implementations and chapters
    ├── common/            # Common data structure implementations (compiled into alg_lib)
    │   └── custom_data_structure.cpp
    ├── ch1/               # Chapter 1 Exercises
    │   └── a.cpp
    └── ch2/               # Chapter 2 Exercises
        ├── b.cpp
        └── c.cpp

⚙️ Environment / Prerequisites

  1. Neovim / VS Code (Recommended)

    This template is optimized for Language Server Protocols (LSP) such as Clangd:

    • compile_commands.json: CMake is configured to automatically generate and mirror this file into your project root. This ensures that your LSP can resolve paths for auto-completion, diagnostics, and navigation instantly.
    • Note: It explicitly avoids copying this file under native MSVC Visual Studio environments to prevent generation conflicts.
  2. Multi-Config vs Single-Config Generators

    Make sure you have CMake (3.15+) and at least one of the following toolchains installed and added to your system PATH:

    • MinGW Makefiles / Ninja + LLVM/Clang: These are Single-Config generators. Debug and Release targets are built into separate dedicated build directories.
    • Visual Studio 17 2022: This is a Multi-Config generator. A single configuration step generates a unified solution (.sln), and you switch between Debug or Release directly at the build stage.

🛠️ Build commands

Since this project utilizes CMake Presets, all options are wrapped into standard names and you can configure and build the project using the simplified preset names without manually specifying generators or build directories.

Building with Ninja (Recommended)

Debug Mode (with debugging symbols):

# Configure the project (generates compile_commands.json in the build directory)
cmake --preset ninja-debug

# Build all targets
cmake --build --preset ninja-debug

Release Mode (optimized):

cmake --preset ninja-release
cmake --build --preset ninja-release

Building with MinGW Makefiles

Debug Mode:

cmake --preset mingw-debug
cmake --build --preset mingw-debug

Release Mode:

cmake --preset mingw-release
cmake --build --preset mingw-release

Building with Visual Studio (MSVC)

Since MSVC is a multi-configuration generator, configuration is done once, and the build type is determined at the build step:

# Configure the project
cmake --preset msvc

# Build Debug version
cmake --build --preset msvc-debug

# Build Release version
cmake --build --preset msvc-release

Compiling & Running a Specific Exercise

To avoid rebuilding the entire project, you can compile a single target (the filename without the extension) by using the --target flag.

Note: Target names are automatically formatted as _ (e.g., src/ch2/b.cpp -> ch2_b).

# Example: Compile only 'ch2_b'
cmake --build --preset ninja-debug --target ch2_b

# Run the compiled executable (Ninja Debug build path example)
./build/ninja/debug/ch2_b.exe

⚡ How Automatic Target Discovery Works (During Build Stage)

In traditional CMake setups, adding a new source file requires manually adding add_executable(...) entries to CMakeLists.txt. This template automates this process using CMake 3.12+ CONFIGURE_DEPENDS and dynamic loop configuration.

  1. File System Watching with CONFIGURE_DEPENDS In CMakeLists.txt, files are dynamically gathered via:

    file(GLOB_RECURSE EXPERIMENT_SOURCES 
        CONFIGURE_DEPENDS
        "${CMAKE_CURRENT_SOURCE_DIR}/src/ch*/*.c"
        "${CMAKE_CURRENT_SOURCE_DIR}/src/ch*/*.cpp"
    )
    

    When CONFIGURE_DEPENDS is specified, CMake injects file system watchers into the underlying build tool (e.g., Ninja or Make).

    When you add, delete, or rename a .c/.cpp file in src/ch*/ and run cmake --build:

    The build tool detects a modification in the directory timestamps.

    It automatically triggers a re-configure step (cmake -B build) before executing the actual compilation.

    CMake updates its build graph and instantly registers your new file as a target.

  2. Dynamic Target & Name Formatting

    Inside the foreach loop, the template parses relative file paths:

    A source file located at src/ch2/b.cpp produces rel_path = ch2/b.cpp.

    The folder ch2 and filename b are merged into a unique target name: ch2_b.

    It then creates the executable target and automatically links alg_lib:

    add_executable(ch2_b src/ch2/b.cpp)
    target_link_libraries(ch2_b PRIVATE alg_lib)