Skip to content

Instantly share code, notes, and snippets.

@JeffMill
Last active November 2, 2024 11:36
Show Gist options
  • Save JeffMill/5be53ecab75579d7f05d8db9b0bce168 to your computer and use it in GitHub Desktop.
Save JeffMill/5be53ecab75579d7f05d8db9b0bce168 to your computer and use it in GitHub Desktop.
Windows build environment using cmake and Visual Studio Build Tools

Windows build environment using cmake, vcpkg, Visual Studio Build Tools

Installation

App Installer (winget)

This is included in Windows 11.

Downlevel: In Microsoft Store, Install "App Installer" (aka winget)

Winget Repo: microsoft/winget-cli: Windows Package Manager CLI (aka winget)

Package Repo: microsoft/winget-pkgs: The Microsoft community Windows Package Manager manifest repository

Git

winget install 'Git.Git'

git.exe config --global user.email '[email protected]'
git.exe config --global user.name 'Your Name'

Build Tools For Visual Studio

Found here -- under "All Downloads", expand "Tools for Visual Studio.

Invoke-WebRequest -Uri 'https://aka.ms/vs/17/release/vs_BuildTools.exe' -OutFile "$env:TEMP\vs_BuildTools.exe"

& "$env:TEMP\vs_BuildTools.exe" --passive --wait --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended --remove Microsoft.VisualStudio.Component.VC.CMake.Project

Note: Build Tools includes CMake, but it won't be in the path if Developer Tools command window isn't open, so we'll install separately. In addition, the website CMake is newer.

Note: It would be nice to use " winget install 'Microsoft.VisualStudio.2022.BuildTools' ", but that installs C# tools, not C++ tools. See this bug

CMake

winget.exe install 'Kitware.CMake'

vcpkg

Note: Installing to `/vcpkg`` to make it easy to access vcpkg.exe, and reduce path lengths.

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

\vcpkg\scripts\bootstrap.ps1 -disableMetrics

Python 3 (optional)

winget.exe install 'Python.Python.3.11'

cmake-format (optional, requires Python)

pip.exe install 'cmake-format'

e.g.,

cmake-format.exe --line-width 100 --tab-size 4 --max-subgroups-hwrap 2 --max-pargs-hwrap 3 --separate-ctrl-name-with-space true --separate-fn-name-with-space true --dangle-parens false --line-ending windows --command-case lower --keyword-case upper --enable-markup false -i CMakeLists.txt

Arguments read from ".cmake-format.json"

clang-format (optional , requires Python)

pip.exe install 'clang-format'

Arguments read from ".clang-format"

cppcheck (optional)

winget.exe install 'Cppcheck.Cppcheck'

doxygen (optional)

winget.exe install 'DimitriVanHeesch.Doxygen'

Test App

Install POCO Library

Poco docs here

\vcpkg\vcpkg.exe search poco

\vcpkg\vcpkg.exe install poco --triplet x64-windows

Note: Ensure you specify triplet for the target platform. You can install multiple triplets, and choose which to build against later.

Note: x64-windows builds x64-windows-dbg and x64-windows-rel.

Create Sample Project

mkdir ~/Repos/md5-vcpkg

Set-Location ~/Repos/md5-vcpkg

CMakeLists.txt

cmake_minimum_required(VERSION 3.5.0)
project(MD5Encrypter)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Poco REQUIRED Foundation)

add_executable(md5 md5.cpp)

# For static linked EXE, add the following:
#
# if(MSVC)
#     add_compile_options(
#         $<$<CONFIG:>:/MT>
#         $<$<CONFIG:Debug>:/MTd>
#         $<$<CONFIG:Release>:/MT>
#     )
# endif()
#
# Note that dependent DLLs (pcre2-8.dll, zlib1.dll) are still required.

target_link_libraries(md5 PRIVATE PRIVATE Poco::Foundation)

md5.cpp

#include "Poco/MD5Engine.h"
#include "Poco/DigestStream.h"

#include <iostream>

int main(int argc, char **argv)
{
    // https://docs.pocoproject.org/current/Poco.MD5Engine.html
    Poco::MD5Engine md5;
    // https://docs.pocoproject.org/current/Poco.DigestOutputStream.html
    Poco::DigestOutputStream ds{md5};
    ds << "abcdefghijklmnopqrstuvwxyz";
    ds.close();

    // https://docs.pocoproject.org/current/Poco.DigestEngine.html
    std::cout << Poco::DigestEngine::digestToHex(md5.digest()) << std::endl;

    return 0;
}

Generate build files with CMake

Note: you don’t need to run any of this in a Visual Studio developer command window.

Set-Location ~/Repos/md5-vcpkg

cmake.exe -DCMAKE_TOOLCHAIN_FILE:FILEPATH='\vcpkg\scripts\buildsystems\vcpkg.cmake' -DVCPKG_TARGET_TRIPLET=x64-windows -S . -B build --graphviz='build\graphviz.dot'

Note: CMAKE_TOOLCHAIN_FILE is the key to getting cmake to use the vcpkg environment.

Note: VCPKG_TARGET_TRIPLET specifies what libraries to use.

Note: ":FILEPATH" suffix helps cmake to deal with paths on Windows, especially in PowerShell

Note: "--graphviz" argument will generate a .DOT file for GraphViz

Build CMake-generated project binary tree

cmake.exe --build build --config RelWithDebInfo --clean-first

View Dependency Graph

Open build\graphviz.dot in Visual Studio, and choose "Graphviz: Toggle Preview"

Run

.\build\RelWithDebInfo\md5.exe
C3fcd3d76192e4007dfb496cca67e13b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment