How to use third-party libraries in C + + projects in Windows system


To use a third-party library, you first need to install it. In novcpkgIn the past, the installation of third-party libraries needed to be compiled and linked locally. If we are novices, we will inevitably encounter problems that are difficult to solve in one way or another. But yesvcpkgIt will be different in the future,vcpkgWill automatically complete all this for us.

Installation vcpkg

withAdministrator privilegesRun PowerShell and execute the commands of clone project, batch file and integration installation:

PS C:\Users\panjie\github> git clone --depth=1
PS C:\Users\panjie\github> cd .\vcpkg\
PS C:\Users\panjie\github\vcpkg> .\bootstrap-vcpkg.bat

Wait a moment, and the prompt of successful installation will be displayed.
vcpkgShould bevc++ package managerShort for, i.eVC + + package manager, it functions like other package managers. But it may be that C + + appeared earlier, so its package manager didn’t do itnpmSuch a manager is so excellent.

Install third party packages

usevcpkgInstalling third-party packages is very simple and requires only executionName of vcpkg install packageJust. If we don’t know the specific name of the package, we can also useName of vcpkg search packageTo search, for example, we want to achievewebsocketFunction, you can perform: .\vcpkg.exe search websocket

PS C:\github\vcpkg> .\vcpkg.exe search websocket
beast                0                HTTP/1 and WebSocket, header-only using Boost.Asio and C++11
brynet               1.11.0           A C++ header only cross platform high performance tcp network library, and sup...
bsio                 1.0.0            networking library, ans support HTTP/WebSocket, based on asio.
cpprestsdk[websockets]                Websockets support
ixwebsocket          11.2.6           Lightweight WebSocket Client and Server + HTTP Client and Server
uwebsockets         Simple, secure & standards compliant web I/O for the most demanding of applica...
websocketpp          0.8.2#1          Library that implements RFC6455 The WebSocket Protocol

Or you can open it

How to use third-party libraries in C + + projects in Windows system

This page gives the package name, version number, main function description and compatibility. clickView DetailsInstallation commands are also given.

For example, we install it in Windows system at this timewebsocketpp, clickView DetailsFollow the prompts given:.\vcpkg install websocketpp
How to use third-party libraries in C + + projects in Windows system

I need it hereParticular attentionThe key points are:vcpkgThe default installed package is32Bit namelyx86-windows, if our current computer is64Bit, it needs to be added after the corresponding package name:x64-windowssuffix. For example, our correct command should be:.\vcpkg install websocketpp:x64-windows

If you just want to learn, you can try to use itvcpkg install libzip:x64-windowsTo install a smaller package:libzip。 If your operating system is 32-bit, it needs to be removed:x64-windowssuffix.

Most of the errors in the installation process are caused by the network. The speed of installation depends on the CPU compilation speed of the computer. All we have to do is wait patiently. If you stay in which interface during installation, you need to view itvcpkgWhether the data is being downloaded (check the network condition) or compiled (check the CPU usage). If both are not, we need to press enter to help it.

vcpkg installDepend oncmake, if not installed on the current computercmakevcpkgWill be automatically downloaded and installed. What we need to ensure is that the current computer network is unblocked.

After successful installationvcpkgYou will be prompted how tocmakeConfigure the current package in the project:

Elapsed time for package websocketpp::x64-windows: 10.13 s

Total elapsed time: 32.09 min

The package websocketpp:x64-windows provides CMake targets:

    find_package(websocketpp CONFIG REQUIRED)
    target_link_libraries(main PRIVATE websocketpp::websocketpp)

If we usecmakeProject, you only need to copy the above two commands and add them toCMakeLists.txtYes.

The successful installation of the third-party package is not the ultimate goal. Our ultimate goal is to load the third-party package into our project.

There are two types of projects: the first is an application developed using Visio studio, which is based onMSBuildConstruction; The second type is based on other toolscmakeApplication.


TocmakeTo use third-party packages in, you need to letcmakeThe third-party package can be found successfully.vcpkgA method forcmakeAdd parameters to achieve this function.
implementvcpkg integrate installAfter, you will get the following prompt information:

PS C:\Users\panjie\github\vcpkg> .\vcpkg.exe integrate install

//If you are under cmkae project, you can add corresponding parameters
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=C:/Users/panjie/github/vcpkg/scripts/buildsystems/vcpkg.cmake"

For example, in existenceCMakeLists.txtExecute on Path:cmake -DCMAKE_TOOLCHAIN_FILE=C:/Users/panjie/github/vcpkg/scripts/buildsystems/vcpkg.cmake

At the same time, you can also add compilation parameters to the IDE, so that the IDE will automatically add the above parameters when compiling. Take clion as an example:

How to use third-party libraries in C + + projects in Windows system

Visio Studio

To use in Visio studiovcpkgTo manage third-party packages, Visio studio needs to be able to successfully find third-party packages. becausevcpkgIt’s also Microsoft’s own thing, so it’s very convenient to use. As long as the version of Visio studio we use is greater than 2015, we can use itvcpkg.exe integrate installTo combine vs with vcpkg.

PS C:\Users\panjie\github\vcpkg> .\vcpkg.exe integrate install
Applied user-wide integration for this vcpkg root.
//All msbuild C + + projects can use #include to use any installed third-party libraries. The implication is that we can directly use #include to reference third-party libraries in Visio studio in the future
All MSBuild C++ projects can now #include any installed libraries.
Linking will be handled automatically.
Installing new libraries will make them instantly available.

It should be noted that if we are using Visio studio, we need to ensure that the English language pack is installed in the current vs. Otherwise, you will get the following warning message:

Warning: The following VS instances are excluded because the English language pack is unavailable.
    C:\Program Files (x86)\Microsoft Visual Studio17\Community
Please install the English language pack.
No suitable Visual Studio instances were found

If you need to install the English language pack for vs, and then execute it again:vcpkg integrate install


After installing the package, you can use the following commands to use the third-party package in the project:

#Include < package name / header file name. H >
#Include < package name / header file name. HPP >

For example:

#include <websocketpp/config/asio_no_tls.hpp>

Note that the above format is#Include < package name / header file name. Suffix >。 If the IDE prompt you use does not find the relevant header file, you may need to restart.


Normally, the above operations are all in accordance with the official documents, and there should be no problem. But there is but, sometimes it just can’t.vcpkgAfter installation, the relevant header files are not found automatically by using Visio studio. Still prompt: can not open source file “XXX. H”.

Some people say it’s time to re executevcpkg.exe integrate installIs this integrated command solved? The answer is no, becausevcpkg.exe integrate installThe function of the command is to add a point to the function of adding third-party dependencies for Visio studiovcpkgLinks, that is, as long asvcpkgWhen a new package is installed, Visio studio will be updated immediately.

So ifvisio studioThere was no immediate update, mostly because we did not install it successfully.

The standard for judging whether a package is successfully installed is also very simple. Let’s open itvcpkgFolder, foundinstalledSubfolder. If the current system is 64 bit, clickx64-windowssee; If it is 32-bit, clickx86-windowssee.

If there is a corresponding generated folder named by a third party, the descriptionvcpkgIf the installation is successful, the problem should be in the integration phase of Visio studio.
How to use third-party libraries in C + + projects in Windows system

If there is no corresponding folder, there is a problem with the installation. This problem is mostly due to forgetting to add it during installation:x64-windowsCaused by suffix.

If we are not interested in using a vs version less than 2015, we may need toWith nuget