<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Vcpkg on KTDEVX</title><link>/en/tags/vcpkg/</link><description>KTDEVX (Vcpkg)</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>ktdevx@gmail.com
(Kaito Takemura)</managingEditor><lastBuildDate>Sun, 29 Sep 2024 23:48:53 +0900</lastBuildDate><atom:link href="/en/tags/vcpkg/index.xml" rel="self" type="application/rss+xml"/><item><title>Easily Install C/C++ Libraries on Windows Using vcpkg</title><link>/en/blog/using-vcpkg-in-windows/</link><pubDate>Sun, 29 Sep 2024 23:48:53 +0900</pubDate><author>ktdevx@gmail.com (Kaito Takemura)</author><guid>/en/blog/using-vcpkg-in-windows/</guid><description>&lt;p>vcpkg is a package management system for C/C++ libraries provided by Microsoft.&lt;/p>
&lt;p>This article explains how to install C/C++ libraries using vcpkg.&lt;/p>
&lt;h2 id="benefits-of-using-vcpkg" >
&lt;div>
&lt;a href="#benefits-of-using-vcpkg">
#
&lt;/a>
Benefits of Using vcpkg
&lt;/div>
&lt;/h2>
&lt;p>Normally, when using an open-source library on Windows, you must build it and resolve its dependencies yourself. If the library has a complex structure, simply installing it can take considerable time.&lt;/p>
&lt;p>With vcpkg, the tool performs these troublesome tasks automatically, making it easy to install libraries. Developers can focus on programming and improve their development efficiency.&lt;/p>
&lt;h2 id="install-vcpkg" >
&lt;div>
&lt;a href="#install-vcpkg">
#
&lt;/a>
Install vcpkg
&lt;/div>
&lt;/h2>
&lt;p>To install vcpkg, download its source code and generate an executable. Although this may sound difficult, the installation is very simple and takes only two steps.&lt;/p>
&lt;h3 id="download-the-source-code" >
&lt;div>
&lt;a href="#download-the-source-code">
##
&lt;/a>
Download the Source Code
&lt;/div>
&lt;/h3>
&lt;p>The vcpkg source code is published on GitHub. Clone the vcpkg repository from GitHub with Git.&lt;/p>
&lt;pre tabindex="0">&lt;code>git clone https://github.com/microsoft/vcpkg.git
&lt;/code>&lt;/pre>&lt;p>The local repository is created in the directory where the clone is run. Move to the directory where you want to install vcpkg beforehand. A short path such as directly under the C drive is recommended.&lt;/p>
&lt;h3 id="generate-the-executable" >
&lt;div>
&lt;a href="#generate-the-executable">
##
&lt;/a>
Generate the Executable
&lt;/div>
&lt;/h3>
&lt;p>A batch file named &lt;code>bootstrap-vcpkg.bat&lt;/code> is stored in the local repository. The batch file automatically generates the executable.&lt;/p>
&lt;pre tabindex="0">&lt;code>cd vcpkg
bootstrap-vcpkg.bat
&lt;/code>&lt;/pre>&lt;p>The vcpkg installation is complete when an executable named &lt;code>vcpkg.exe&lt;/code> is generated in the local repository.&lt;/p>
&lt;p>It is convenient to add the path to the executable to an environment variable so that it can be called from any directory.&lt;/p>
&lt;h2 id="basic-vcpkg-operations" >
&lt;div>
&lt;a href="#basic-vcpkg-operations">
#
&lt;/a>
Basic vcpkg Operations
&lt;/div>
&lt;/h2>
&lt;h3 id="search-for-available-libraries" >
&lt;div>
&lt;a href="#search-for-available-libraries">
##
&lt;/a>
Search for Available Libraries
&lt;/div>
&lt;/h3>
&lt;p>Search for available libraries with the &lt;code>vcpkg search&lt;/code> command.&lt;/p>
&lt;pre tabindex="0">&lt;code>vcpkg search [&amp;lt;package-name&amp;gt;]
&lt;/code>&lt;/pre>&lt;p>Specifying a package name displays only matching libraries. If no name is specified, all available libraries are displayed.&lt;/p>
&lt;p>You can also search the available libraries on the &lt;a href="https://vcpkg.io/en/packages.html">package list&lt;/a>.&lt;/p>
&lt;h3 id="install-a-library" >
&lt;div>
&lt;a href="#install-a-library">
##
&lt;/a>
Install a Library
&lt;/div>
&lt;/h3>
&lt;p>Install a library with the &lt;code>vcpkg install&lt;/code> command.&lt;/p>
&lt;pre tabindex="0">&lt;code>vcpkg install &amp;lt;package-name&amp;gt;
&lt;/code>&lt;/pre>&lt;h3 id="list-installed-libraries" >
&lt;div>
&lt;a href="#list-installed-libraries">
##
&lt;/a>
List Installed Libraries
&lt;/div>
&lt;/h3>
&lt;p>List installed libraries with the &lt;code>vcpkg list&lt;/code> command.&lt;/p>
&lt;pre tabindex="0">&lt;code>vcpkg list [&amp;lt;package-name&amp;gt;]
&lt;/code>&lt;/pre>&lt;p>Specifying a package name displays only matching libraries. If no name is specified, all installed libraries are displayed.&lt;/p>
&lt;h3 id="uninstall-a-library" >
&lt;div>
&lt;a href="#uninstall-a-library">
##
&lt;/a>
Uninstall a Library
&lt;/div>
&lt;/h3>
&lt;p>Uninstall a library with the &lt;code>vcpkg remove&lt;/code> command.&lt;/p>
&lt;pre tabindex="0">&lt;code>vcpkg remove &amp;lt;package-name&amp;gt;
&lt;/code>&lt;/pre>&lt;h2 id="specify-the-target-environment" >
&lt;div>
&lt;a href="#specify-the-target-environment">
#
&lt;/a>
Specify the Target Environment
&lt;/div>
&lt;/h2>
&lt;p>vcpkg provides cross-compilation capabilities.&lt;/p>
&lt;p>The triplet option allows you to install or uninstall libraries intended to run in a specified environment.&lt;/p>
&lt;p>A triplet is a string representing the target environment. It can specify the CPU, OS, and library linking method.&lt;/p>
&lt;pre tabindex="0">&lt;code>vcpkg install &amp;lt;package-name&amp;gt; --triplet=&amp;lt;target-environment&amp;gt;
vcpkg install &amp;lt;package-name&amp;gt;:&amp;lt;target-environment&amp;gt;
&lt;/code>&lt;/pre>&lt;p>You can check the supported target environments with &lt;code>vcpkg help triplet&lt;/code>.&lt;/p>
&lt;h2 id="use-installed-libraries" >
&lt;div>
&lt;a href="#use-installed-libraries">
#
&lt;/a>
Use Installed Libraries
&lt;/div>
&lt;/h2>
&lt;h3 id="use-from-visual-studio" >
&lt;div>
&lt;a href="#use-from-visual-studio">
##
&lt;/a>
Use from Visual Studio
&lt;/div>
&lt;/h3>
&lt;p>Use libraries installed with vcpkg from Visual Studio with the &lt;code>vcpkg integrate&lt;/code> command.&lt;/p>
&lt;pre tabindex="0">&lt;code>vcpkg integrate install
&lt;/code>&lt;/pre>&lt;p>This automatically adds the include directories, library directories, and libraries to Visual Studio.&lt;/p>
&lt;p>Open the project in Visual Studio and change the target environment by configuring the triplet in the project properties.&lt;/p>
&lt;p>&lt;img alt="Visual Studio properties page" src="https://www.ktdevx.com/ja/blog/using-vcpkg-in-windows/images/visual-studio-settings.webp">&lt;/p>
&lt;h3 id="use-from-cmake" >
&lt;div>
&lt;a href="#use-from-cmake">
##
&lt;/a>
Use from CMake
&lt;/div>
&lt;/h3>
&lt;p>Use libraries installed with vcpkg from CMake by setting the path to &lt;code>vcpkg.cmake&lt;/code> in the &lt;code>CMAKE_TOOLCHAIN_FILE&lt;/code> variable.&lt;/p>
&lt;pre tabindex="0">&lt;code>cmake [&amp;lt;path-to-project&amp;gt;] -D CMAKE_TOOLCHAIN_FILE=&amp;lt;path-to-vcpkg&amp;gt;/scripts/buildsystems/vcpkg.cmake
&lt;/code>&lt;/pre>&lt;p>CMake can now find the libraries.&lt;/p>
&lt;p>Change the target environment by setting the &lt;code>VCPKG_TARGET_TRIPLET&lt;/code> variable.&lt;/p>
&lt;pre tabindex="0">&lt;code>cmake [&amp;lt;path-to-project&amp;gt;] -D VCPKG_TARGET_TRIPLET=&amp;lt;target-environment&amp;gt; -D CMAKE_TOOLCHAIN_FILE=&amp;lt;path-to-vcpkg.cmake&amp;gt;
&lt;/code>&lt;/pre>&lt;h3 id="use-from-other-development-environments" >
&lt;div>
&lt;a href="#use-from-other-development-environments">
##
&lt;/a>
Use from Other Development Environments
&lt;/div>
&lt;/h3>
&lt;p>Installed libraries are stored in the &lt;code>vcpkg/installed/&amp;lt;target-environment&amp;gt;&lt;/code> directory.&lt;/p>
&lt;p>You can use libraries installed with vcpkg by specifying the paths appropriately in the consuming environment.&lt;/p>
&lt;h2 id="external-links" >
&lt;div>
&lt;a href="#external-links">
#
&lt;/a>
External Links
&lt;/div>
&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://vcpkg.io/en/">vcpkg - Open source C/C++ dependency manager from Microsoft&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://github.com/microsoft/vcpkg">GitHub - microsoft/vcpkg: C++ Library Manager for Windows, Linux, and MacOS&lt;/a>&lt;/li>
&lt;/ul></description></item></channel></rss>