Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Setting Up a CMake + vcpkg + Ninja Build Toolchain on macOS

Tech May 15 1

Installing Prrerequisites

Installing Homebrew

Homebrew serves as the primary package manager for macOS, providing a convenient way to install development tools. Execute the following command in you're terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Installing Build Tools

Both CMake and Ninja are available through Homebrew:

brew install cmake ninja

Installing vcpkg

Manual installation of vcpkg is recommended since the absolute path will be required for configuration. Choose a directory to installation, such as your home directory.

Clone the vcpkg repository:

git clone https://github.com/microsoft/vcpkg.git

Run the bootstrap script:

cd vcpkg
./bootstrap-vcpkg.sh

Configuring Environment Variables

For zsh users, edit your shell configuration:

nano ~/.zshrc

Add the following at the beginning of the file:

export PATH="$HOME/vcpkg:$PATH"

Add the vcpkg root directory variable:

export VCPKG_ROOT="$HOME/vcpkg"

Enabling CMake Integration

Run the integration command:

vcpkg integrate install

Note the CMake toolchain file path from the output:

Applied user-wide integration for this vcpkg root. CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake"

Project Configuration

Creating a vcpkg Project

Initialize a new vcpkg manifest:

vcpkg new --application

Add a dependency for testing:

vcpkg add port fmt

CMakeLists.txt Configuration

Create a CMakeLists.txt file with the following content:

cmake_minimum_required(VERSION 4.2)
project(DemoApp)

set(CMAKE_CXX_STANDARD 20)

add_executable(DemoApp src/main.cpp)

find_package(fmt CONFIG REQUIRED)
target_link_libraries(DemoApp PRIVATE fmt::fmt)

CMakePresets.json Configuration

Create a CMakePresets.json file:

{
    "version": 8,
    "configurePresets": [
        {
            "name": "Debug",
            "generator": "Ninja",
            "displayName": "Clang-Debug",
            "binaryDir": "${sourceDir}/build-debug",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug",
                "CMAKE_C_COMPILER": "/usr/bin/clang",
                "CMAKE_CXX_COMPILER": "/usr/bin/clang++",
                "CMAKE_MAKE_PROGRAM": "/opt/homebrew/bin/ninja",
                "CMAKE_TOOLCHAIN_FILE": "$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake"
            }
        },
        {
            "name": "Release",
            "generator": "Ninja",
            "displayName": "Clang-Release",
            "binaryDir": "${sourceDir}/build-release",
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release",
                "CMAKE_C_COMPILER": "/usr/bin/clang",
                "CMAKE_CXX_COMPILER": "/usr/bin/clang++",
                "CMAKE_MAKE_PROGRAM": "/opt/homebrew/bin/ninja",
                "CMAKE_TOOLCHAIN_FILE": "$HOME/vcpkg/scripts/buildsystems/vcpkg.cmake"
            }
        }
    ]
}

Source File

Create the source file at src/main.cpp:

#include <fmt/core.h>

int main() {
    fmt::print("Hello from vcpkg!\n");
    return 0;
}

Building the Project

Generate the build directory using a preset:

cmake -B build --preset Debug

Compile the project:

cmake --build build

The compiled binary will be located in the build directory.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.