title | description | zone_pivot_group_filename | zone_pivot_groups | author | ms.author | ms.topic | ms.date |
---|---|---|---|---|---|---|---|
Install and use packages with CMake |
Tutorial guides the user through the process of installing and using packages with CMake. |
zone-pivot-groups.json |
shell-selections |
JavierMatosD |
javiermat |
tutorial |
7/16/2024 |
This tutorial shows you how to create a C++ "Hello World" program that uses the fmt
library with CMake and vcpkg. You'll install dependencies, configure, build, and run a simple application.
::: zone pivot="shell-cmd, shell-powershell"
Note
For Windows users, Visual Studio's MSVC (Microsoft Visual C++ Compiler) is the required compiler for C++ development.
::: zone-end
[!INCLUDE setup-vcpkg]
-
Configure the
VCPKG_ROOT
environment variable.::: zone pivot="shell-bash"
export VCPKG_ROOT=/path/to/vcpkg export PATH=$VCPKG_ROOT:$PATH
[!INCLUDE env-vars]
::: zone-end
::: zone pivot="shell-cmd"
set "VCPKG_ROOT=C:\path\to\vcpkg" set PATH=%VCPKG_ROOT%;%PATH%
[!INCLUDE env-vars]
::: zone-end ::: zone pivot="shell-powershell"
$env:VCPKG_ROOT = "C:\path\to\vcpkg" $env:PATH = "$env:VCPKG_ROOT;$env:PATH"
[!INCLUDE env-vars]
::: zone-end
Setting
VCPKG_ROOT
tells vcpkg where your vcpkg instance is located. Adding it toPATH
ensures you can run vcpkg commands directly from the shell. -
Create the project directory.
mkdir helloworld && cd helloworld
-
Create the manifest file and add the
fmt
dependency.First, create a manifest file (
vcpkg.json
) in your project's directory by running thevcpkg new
command from within thehelloworld
directory:vcpkg new --application
Next, add the
fmt
dependency:vcpkg add port fmt
Your
vcpkg.json
should look like this:{ "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 default
vcpkg-configuration.json
file introduces baseline constraints, specifying the minimum versions of dependencies that your project should use. While modifying this file is beyond the scope of this tutorial, it plays a crucial role in defining version constraints for your project's dependencies. Therefore, even though it's not strictly necessary for this tutorial, it's a good practice to addvcpkg-configuration.json
to your source control to ensure version consistency across different development environments. -
Create the project files.
Create the
CMakeLists.txt
file with the following content::::code language="cmake" source="../examples/snippets/get-started/CMakeLists.txt":::
Now, let's break down what each line in the
CMakeLists.txt
file 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, an error will be generated.project(HelloWorld)
: Sets the name of the project to "HelloWorld."find_package(fmt CONFIG REQUIRED)
: Looks for thefmt
library using its CMake configuration file. TheREQUIRED
keyword ensures that an error is generated if the package is not found.add_executable(HelloWorld main.cpp)
: Adds an executable target named "HelloWorld," built from the source filemain.cpp
.target_link_libraries(HelloWorld PRIVATE fmt::fmt)
: Specifies that theHelloWorld
executable should link against thefmt
library. ThePRIVATE
keyword indicates thatfmt
is only needed for buildingHelloWorld
and should not propagate to other dependent projects.
Create the
main.cpp
file with the following content::::code language="cpp" source="../examples/snippets/get-started/main.cpp":::
In this
main.cpp
file, the<fmt/core.h>
header is included for using thefmt
library. Themain()
function then callsfmt::print()
to output the "Hello World!" message to the console.
-
Run CMake configuration
To allow the CMake project system to recognize C++ libraries provided by vcpkg, you'll need to provide the
vcpkg.cmake
toolchain file. To automate this, create aCMakePresets.json
file in the "helloworld" directory with the following content::::code language="cmake" source="../examples/snippets/get-started/CMakePresets.json":::
This
CMakePresets.json
file contains a single "default" preset for CMake and sets theCMAKE_TOOLCHAIN_FILE
variable. TheCMAKE_TOOLCHAIN_FILE
allows the CMake project system to recognize C++ libraries provided by vcpkg. Adding theCMakePresets.json
automates the process of specifying the toolchain when running CMake.Configure the build using CMake:
cmake --preset=default
-
Build the project
Run:
cmake --build build
-
Run the application
Finally, run the executable to see your application in action:
::: zone pivot="shell-bash"
./build/HelloWorld Hello World!
::: zone-end
::: zone pivot="shell-cmd, shell-powershell"
.\build\HelloWorld.exe Hello World!
::: zone-end