Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this tutorial, we'll walk through how to export compiled dependencies using vcpkg. This can be particularly useful when you want to share libraries across multiple projects or for ease of distribution. Additionally, the process can be adapted for offline or air-gapped scenarios by transferring the exported archive to environments without live internet connectivity.
Prerequisites
- Visual Studio with C++ development workload and CMake component
- Git
- vcpkg
1 - Install the fmt
library
Open a terminal and navigate to the directory where vcpkg is installed then install the fmt
library for your target platform:
vcpkg install fmt:x64-windows
Replace x64-windows
with your target triplet.
2 - Export the fmt
library
After installing fmt
, export it by running:
vcpkg export fmt:x64-windows --zip
You should see output similar to the following:
The following packages are already built and will be exported:
fmt:x64-windows
* vcpkg-cmake:x64-windows
* vcpkg-cmake-config:x64-windows
Additional packages (*) need to be exported to complete this operation.
Exporting vcpkg-cmake:x64-windows...
Exporting vcpkg-cmake-config:x64-windows...
Exporting fmt:x64-windows...
Creating zip archive...
Zip archive exported at: C:\dev\vcpkg\vcpkg-export-20231106-164550.zip
To use exported libraries in CMake projects, add -DCMAKE_TOOLCHAIN_FILE=[...]/scripts/buildsystems/vcpkg.cmake to your CMake command line.
The command creates a zip archive containing all the necessary files to use the fmt
library, including binaries and headers. This exported archive can be transferred to offline systems for deployment. For a list of all the supported formats check out the export
command documentation.
3 - Use the fmt
library in a new CMake project
To use the fmt
library in a new CMake project, follow these steps:
Unzip the exported archive to a known location on your machine, or transfer it to your offline environment.
Make a new directory for your CMake project and navigate into it.
Create a
CMakeLists.txt
with the following content: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)
Write your
main.cpp
file to includefmt
and print "Hello, World":#include <fmt/core.h> int main() { fmt::print("Hello, World!\n"); return 0; }
Build the project - In your terminal, from the project directory, run:
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=<path-to-archive>/scripts/buildsystems/vcpkg.cmake cmake --build build
Execute:
..\build\Debug\HelloWorld.exe
Next Steps
And that's it! You have exported the fmt
library and used it in a separate project.