Skip to content

Instantly share code, notes, and snippets.

@milnak
Last active June 8, 2024 05:30
Show Gist options
  • Save milnak/67e5b7bf036c26827a8eee2911028e37 to your computer and use it in GitHub Desktop.
Save milnak/67e5b7bf036c26827a8eee2911028e37 to your computer and use it in GitHub Desktop.
Install Visual Studio Build Tools

Visual Studio VC Build Tools w/vcpkg, CMake and Ninja, plus Qt.

Install Build Tools

Build Tools Homepage -- expand "Tools for Visual Studio".

Build Tools Component Directory

Microsoft.VisualStudio.Workload.VCTools (Desktop development with C++):

  • Microsoft.Component.MSBuild (MSBuild)
  • Microsoft.VisualStudio.Component.TextTemplating (Text Template Transformation)
  • Microsoft.VisualStudio.Component.VC.CoreBuildTools (C++ Build Tools core features)
  • Microsoft.VisualStudio.Component.VC.CoreIde (C++ core features)
  • Microsoft.VisualStudio.Component.VC.Redist.14.Latest (C++ 2022 Redistributable Update)
  • Microsoft.VisualStudio.ComponentGroup.NativeDesktop.Core (C++ core desktop features)

Recommended (--includeRecommended):

  • Microsoft.VisualStudio.Component.TestTools.BuildTools (Testing tools core features - Build Tools)
  • Microsoft.VisualStudio.Component.VC.ASAN (C++ AddressSanitzer)
  • Microsoft.VisualStudio.Component.VC.CMake.Project (C++ CMake tools for Windows)
  • Microsoft.VisualStudio.Component.VC.Tools.x86.x64 (MSVC v143 - VS 2022 C++ x64/x86 build tools (Latest))
  • Microsoft.VisualStudio.Component.Windows11SDK.22621 (Windows 11 SDK (10.0.22621.0)
winget.exe install `
  --id Microsoft.VisualStudio.2022.BuildTools `
  --override '--passive --wait --add Microsoft.VisualStudio.Workload.VCTools --add Microsoft.VisualStudio.Component.VC.CMake.Project --add Microsoft.VisualStudio.Component.Windows11SDK.22621'

Install Paths:

  • MSVC: C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.40.33807
  • CMake: C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake
  • Ninja: C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja
  • Windows Kits: C:\Program Files (x86)\Windows Kits\10

Verify Install

Install vcpkg

Browse vcpkg public packages

mkdir \src; cd \src

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

.\vcpkg\scripts\bootstrap.ps1 -disableMetrics

\src\DevCmd.ps1

# Set env:VCPKG_ROOT to vcpkg installation path.
param([string]$InstallPath = (Join-Path $PSScriptRoot 'vcpkg'))

$env:VCPKG_ROOT = $InstallPath

if (!(Test-Path $env:VCPKG_ROOT -PathType Container)) {
  throw 'vcpkg not installed.'
}

$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
if (!(Test-Path $vswhere -PathType Leaf)) {
  throw 'vswhere not installed.'
}

# This can alternately be specified in "CMakePresets.json" as:
# "cacheVariables": { "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" }
$env:CMAKE_TOOLCHAIN_FILE = "$env:VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake"

# CXX,INCLUDE,LIB only needed for Ninja builds. Unclear why ninja needs this but MSVC doesn't.

$buildToolsPath = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -Property installationPath
if (!$buildToolsPath -or !(Test-Path $buildToolsPath -PathType Container)) {
  throw "$buildToolsPath doesnt exist."
}
$buildToolsPath = $buildToolsPath.TrimEnd('\\')
$buildToolsPathFwd = $buildToolsPath -replace '\\','/'

$kitsRootPath = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots' -Name 'KitsRoot10').KitsRoot10
if (!$kitsRootPath -or !(Test-Path $kitsRootPath -PathType Container)) {
  throw "$kitsRootPath doesnt exist."
}
$kitsRootPath = $kitsRootPath.TrimEnd('\\')
$kitsRootPathFwd = $kitsRootPath -replace '\\','/'

$env:CXX = $buildToolsPathFwd + '/VC/Tools/MSVC/14.40.33807/bin/Hostx64/x64/cl.exe'
$env:CC = $env:CXX

$env:INCLUDE = $buildToolsPathFwd + '/VC/Tools/MSVC/14.40.33807/include'
'cppwinrt','shared','ucrt','um','winrt' | ForEach-Object {
  $env:INCLUDE += ';' + $kitsRootPathFwd + "/Include/10.0.22621.0/$_"
}

$env:INCLUDE -split ';' | ForEach-Object {
  if (-not (Test-Path $_ -PathType Container)) {
    Write-Warning "Path not found: $_"
  }
}

$env:LIB = $buildToolsPathFwd + '/VC/Tools/MSVC/14.40.33807/lib/x64'
'ucrt','um' | ForEach-Object {
  $env:LIB += ';' + $kitsRootPathFwd + "/Lib/10.0.22621.0/$_/x64"
}

$env:LIB -split ';' | ForEach-Object {
  if (-not (Test-Path $_ -PathType Container)) {
    Write-Warning "Path not found: $_"
  }
}

# Add VCPKG, Kits (rc.exe), Tools (dumpbin.exe) cmake, ninja to env:PATH
$env:PATH += ';' + $env:VCPKG_ROOT
$env:PATH += ';' + $kitsRootPath + '\bin\10.0.22621.0\x64'
$env:PATH += ';' + $buildToolsPath + '\VC\Tools\MSVC\14.40.33807\bin\Hostx64\x64'
$env:PATH += ';' + $buildToolsPath + '\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin'
$env:PATH += ';' + $buildToolsPath + '\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja'

$env:PATH -split ';' | ForEach-Object {
  if (-not (Test-Path $_ -PathType Container)) {
    Write-Warning "Path not found: $_"
  }
}

Initialize dev environment

\src\devcmd.ps1

Create Project

From Tutorial: Install and use packages with vcpkg

mkdir helloworld; cd helloworld

vcpkg.exe new --application

vcpkg.exe add port fmt

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)
project(HelloWorld)
find_package(fmt CONFIG REQUIRED)
add_executable(HelloWorld main.cpp)
target_link_libraries(HelloWorld PRIVATE fmt::fmt)

main.cpp

#include <fmt/core.h>

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

CMakePresets.json

{
  "version": 2,
  "configurePresets": [
    {
      "name": "default",
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build",
      "cacheVariables": {
        "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
      }
    }
  ]
}

Build and Run (msvc)

cd \src\helloworld

cmake.exe -B build

cmake.exe --build build

.\build\Debug\HelloWorld.exe

Build and Run (presets / ninja)

cd \src\helloworld

# Adding `--debug-output` can help debug issues.
cmake.exe --preset=default

ninja.exe -C build

.\build\HelloWorld.exe

Building Qt Project

See Getting started with CMake.

Clone Qt Project

cd \src

git.exe clone https://github.com/alcroito/qt_world_summit_2019_cmake_vcpkg_app.git

cd qt_world_summit_2019_cmake_vcpkg_app

Fix CMakeLists.txt

find_package(botan CONFIG REQUIRED)
...
set(CMAKE_CXX_STANDARD 20)

Install qt5 and botan

vcpkg.exe install qt5

vcpkg.exe install botan

Build and Run Project

mkdir builds_win; cd builds_win

cmake.exe .. -GNinja

ninja.exe

./encrypted_texteditor.exe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment