This tutorial shows you how to create a C++ "Hello World" program that uses the fmt library with CMake, vcpkg and Visual Studio. You'll install dependencies, configure, build, and run a simple application.
- Visual Studio with C++ development workload and CMake component
- Git
The first step is to clone the vcpkg repository from GitHub. The repository contains scripts to acquire the vcpkg executable and a registry of curated open-source libraries maintained by the vcpkg community. To do this, run:
git clone https://github.com/microsoft/vcpkg.gitThe vcpkg curated registry is a set of over 2,000 open-source libraries. These libraries have been validated by vcpkg's continuous integration pipelines to work together. While the vcpkg repository does not contain the source code for these libraries, it holds recipes and metadata to build and install them in your system.
Now that you have cloned the vcpkg repository, navigate to the vcpkg directory and execute the bootstrap script:
cd vcpkg && bootstrap-vcpkg.bat
or
cd vcpkg; .\bootstrap-vcpkg.bat
cd vcpkg && ./bootstrap-vcpkg.sh
The bootstrap script performs prerequisite checks and downloads the vcpkg executable.
That's it! vcpkg is set up and ready to use.
-
Create the Visual Studio project
- Create a new project in Visual Studio using the "CMake Project" template
- Name your project "helloworld"
- Check the box for "Place solution and project in the same directory."
- Click the "Create" button
-
Configure the
VCPKG_ROOTenvironment variable.Open the built-in Developer PowerShell window in Visual Studio.
Run the following commands:
$env:VCPKG_ROOT = "C:\path\to\vcpkg" $env:PATH = "$env:VCPKG_ROOT;$env:PATH"
Open the Developer command prompt in Visual Studio.
Run the following commands:
set VCPKG_ROOT="C:\path\to\vcpkg" set PATH=%VCPKG_ROOT%;%PATH%
Setting environment variables in this manner only affects the current terminal session. To make these changes permanent across all sessions, set them through the Windows System Environment Variables panel.
Setting
VCPKG_ROOThelps Visual Studio locate your vcpkg instance. Adding it toPATHensures you can run vcpkg commands directly from the shell. -
Generate a manifest file and add dependencies.
Run the following command to create a vcpkg manifest file (
vcpkg.json):vcpkg new --applicationThe
vcpkg newcommand adds avcpkg.jsonfile and avcpkg-configuration.jsonfile in the project's directory.Add the
fmtpackage as a dependency:vcpkg add port fmtYour
vcpkg.jsonshould now contain:{ "dependencies": [ "fmt" ] }This is your manifest file. vcpkg reads the manifest file to learn what dependencies to install and integrates with CMake to provide the dependencies required by your project.
The generated
vcpkg-configuration.jsonfile introduces a baseline that places minimum version constraints on the project's dependencies. Modifying this file is beyond the scope of this tutorial. While not applicable in this tutorial, it's a good practice to keep thevcpkg-configuration.jsonfile under source control to ensure version consistency across different development environments.
-
Modify the
helloworld.cppfile.Replace the content of
helloworld.cppwith the following code:#include <fmt/core.h> int main() { fmt::print("Hello World!\n"); return 0; }
This source file includes the
<fmt/core.h>header which is part of thefmtlibrary. Themain()function callsfmt::print()to output the "Hello World!" message to the console. -
Configure the
CMakePresets.jsonfile.- Rename the
CMakePresets.jsonfile toCMakeUserPresets.json - Update its contents as shown below. Replace
<VCPKG_ROOT>with the path to your vcpkg directory.
{ "version": 2, "configurePresets": [ { "name": "default", "generator": "Ninja", "binaryDir": "${sourceDir}/build", "cacheVariables": { "CMAKE_TOOLCHAIN_FILE": "<VCPKG_ROOT>/scripts/buildsystems/vcpkg.cmake" } } ] }- Since this file includes a hardcoded absolute path, it is recommended that you don't keep this file under source control. If you're using Git, add
CMakeUserPresets.jsonto your.gitignorefile.
The
CMakeUserPresets.jsonfile contains a single preset named "default", this preset sets theCMAKE_TOOLCHAIN_FILEto use vcpkg's CMake toolchain file. This lets vcpkg provide packages to CMake when you configure and build the project. Read the vcpkg CMake integration documentation to learn more. - Rename the
-
Edit the
CMakeLists.txtfile.Replace the contents of the
CMakeLists.txtfile with the following code: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)
Now, let's break down what each line in the
CMakeLists.txtfile does:cmake_minimum_required(VERSION 3.10): Specifies that the minimum version of CMake required to build the project is 3.10. If the version of CMake installed on your system is lower than this, the build fails.project(HelloWorld): Sets the name of the project to "HelloWorld."find_package(fmt CONFIG REQUIRED): Looks for thefmtlibrary using its CMake configuration file. TheREQUIREDkeyword ensures that an error is generated if the package is not found.add_executable(HelloWorld helloworld.cpp): Adds an executable target named "HelloWorld," built from the source filehelloworld.cpp.target_link_libraries(HelloWorld PRIVATE fmt::fmt): Specifies that theHelloWorldexecutable should link against thefmtlibrary. ThePRIVATEkeyword indicates thatfmtis only needed for buildingHelloWorldand should not propagate to other dependent projects.
-
Build the project.
Press
Ctrl+Shift+Bto build the project in Visual Studio. -
Run the application.
To learn more about vcpkg.json, see our reference documentation: