Skip to main content

Install and use packages with MSBuild in Visual Studio

This tutorial shows you how to create a C++ "Hello World" program that uses the fmt library with MSBuild, kmpkg, and Visual Studio. You'll install dependencies, configure the project, build, and run a simple application.

Prerequisites

1 - Set up kmpkg

  1. Clone the repository

    The first step is to clone the kmpkg repository from GitHub. The repository contains scripts to acquire the kmpkg executable and a registry of curated open-source libraries maintained by the kmpkg community. To do this, run:

    git clone https://github.com/kumose/kmpkg.git

    The kmpkg curated registry is a set of over 2,000 open-source libraries. These libraries have been validated by kmpkg's continuous integration pipelines to work together. While the kmpkg repository does not contain the source code for these libraries, it holds recipes and metadata to build and install them in your system.

  2. Run the bootstrap script

    Now that you have cloned the kmpkg repository, navigate to the kmpkg directory and execute the bootstrap script:

cd kmpkg && bootstrap-kmpkg.bat

The bootstrap script performs prerequisite checks and downloads the kmpkg executable.

That's it! kmpkg is set up and ready to use.

  1. Integrate with Visual Studio MSBuild

    The next step is to enable user-wide kmpkg integration, this makes MSBuild aware of kmpkg's installation path.

    Run

    .\kmpkg.exe integrate install

    This outputs:

    All MSBuild C++ projects can now #include any installed libraries. Linking will be handled automatically. Installing new libraries will make them instantly available.

2 - Set up the Visual Studio project

  1. Create the Visual Studio project

    • Create a new project in Visual Studio using the "Console Application" template
  • Name your project "helloworld"

  • Check the box for "Place solution and project in the same directory."

  • Click the "Create" button

  1. Configure the KMPKG_ROOT environment variable.

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.

Open the built-in Developer PowerShell window in Visual Studio.

Run the following commands:

$env:KMPKG_ROOT = "C:\path\to\kmpkg"
$env:PATH = "$env:KMPKG_ROOT;$env:PATH"

Setting KMPKG_ROOT helps Visual Studio locate your kmpkg instance. Adding it to PATH ensures you can run kmpkg commands directly from the shell.

  1. Generate a manifest file and add dependencies.

    Run the following command to create a kmpkg manifest file (kmpkg.json):

    kmpkg new --application

    The kmpkg new command adds a kmpkg.json file and a kmpkg-configuration.json file in the project's directory.

    Add the fmt package as a dependency:

    kmpkg add port fmt

    Your kmpkg.json should now contain:

    {
    "dependencies": [
    "fmt"
    ]
    }

    This is your manifest file. kmpkg reads the manifest file to learn what dependencies to install and integrates with MSBuild to provide the dependencies required by your project.

    The generated kmpkg-configuration.json file 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 the kmpkg-configuration.json file under source control to ensure version consistency across different development environments.

3 - Set up the project files

  1. Modify the helloworld.cpp file.

    Replace the content of helloworld.cpp with 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 the fmt library. The main() function calls fmt::print() to output the "Hello World!" message to the console.

info

The code editor may underline the lines referencing fmt files and symbols as errors. You need to build your project once for kmpkg to install the dependencies and make auto-completion tools evaluate the code correctly.

4 - Enable manifest mode

  1. Navigate to your Project Properties page.

    Using the menu navigation at the top, choose Project > Properties. A new window will open.

  2. Navigate to Configuration Properties > kmpkg, and set Use kmpkg Manifest to Yes.

Other settings, such as triplets, are filled in with default values kmpkg detects from your project and will be useful when configuring your project.

5 - Build and run the project

  1. Build the project.

    Build the project using the Build > Build Solution option from the top menu.

    If MSBuild detects a kmpkg.json file and manifests are enabled in your project, MSBuild installs the manifest's dependencies as a pre-build step. Dependencies are installed in a kmpkg_installed directory in the project's build output directory. Any headers installed by the library can be directly used, and any libraries installed will be automatically linked.

  2. Run the application.

    Finally, run the executable:

You should see the output:

Next steps

To learn more about kmpkg.json and kmpkg MSBuild integration, see our reference documentation: