1

To use third-party libraries, first need to be installed. Before vcpkg , 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. But with vcpkg it will be different. vcpkg will automatically complete all this for us.

Install vcpkg

Run powershell with administrator authority , execute the clone project, run the batch file, and run the integrated installation commands:

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

Wait for a while until the installation is successful.
vcpkg should be vc++ package manager , that is, vc++ package manager, its role is the same as other package managers. But it may be due to the early appearance of C++, so its package manager is not as npm as a manager like 060b031a914856.

Install third-party packages

It vcpkg install third-party packages. You only need to execute the name of the vcpkg install package. If we don’t know the specific name of the package, we can also use the name of the vcpkg search package to search. For example websocket function, we can execute: .\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          19.0.0.5         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 https://vcpkg.io/en/packages.html to search.

image.png

This page gives the package name, version number, main function description and compatibility. Click View Details will also give the installation command.

websocketpp in the windows system at this time, follow the prompt given View Details .\vcpkg install websocketpp
image.png

Here need pay special attention to is: vcpkg default installed package is 32 bit that is x86-windows , if our current computer is 64 bit, we need to add :x64-windows suffix after the corresponding package name. For example, at this time our correct command should be: .\vcpkg install websocketpp:x64-windows

If you are just for learning, you can try to use vcpkg install libzip:x64-windows to install a smaller package: libzip . If your operating system is 32-bit, you need to remove the :x64-windows suffix.

Errors in the installation process are mostly caused by network reasons. The speed of installation depends on the compilation speed of the computer's CPU. All we have to do is to wait patiently. If you stay on which interface during the installation process, you need to check vcpkg is downloading data (checking the network situation) or compiling (checking the CPU usage), if both are not, we need to press Enter Come and help it.

vcpkg install dependent on cmake , should the current computer is not installed cmake , vcpkg will automatically download and install. What we need to ensure is that the current computer network is unblocked.

After successful installation, vcpkg will prompt how to configure the current package cmake

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 are using the cmake project, we only need to copy the above two commands and add them to CMakeLists.txt .

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

As for the projects, there are two categories: the first category is an application developed using visio studio, which is based on MSBuild ; the second category is an application cmake

Cmake

If you cmake to use a third-party package cmake to successfully find the third-party package. vcpkg uses a cmake to achieve this function.
After executing vcpkg integrate install , you will get the following prompt message:

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

// 如果在cmkae项目下,添加对应的参数即可
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=C:/Users/panjie/github/vcpkg/scripts/buildsystems/vcpkg.cmake"

For example, execute on a path CMakeLists.txt 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:

image.png

Visio Studio

If you want to use vcpkg in Visio Studio, you need to enable Visio Studio to successfully find the third-party package. Since vcpkg is also Microsoft's own thing, it is also quite convenient to use. As long as the version of Visio Studio we use is greater than 2015, we can use vcpkg.exe integrate install to combine vs and vcpkg.

PS C:\Users\panjie\github\vcpkg> .\vcpkg.exe integrate install
Applied user-wide integration for this vcpkg root.
// 所有的MSBuild c++工程可以使用#include来使用任意安装的第三方库了,言外之意在说,我们以后在visio studio中可以直接使用#include的方式来引用第三方库
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 Studio\2017\Community
Please install the English language pack.
No suitable Visual Studio instances were found

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

use

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

#include <包名/头文件名.h>
或
#include <包名/头文件名.hpp>

such as:

#include <websocketpp/config/asio_no_tls.hpp>

Note that the above format is #include <package name/header file name. suffix>. If the IDE you are using prompts that the relevant header file is not found, you may need to restart it.

but

Normally, the above operations are all based on official documents, and there should be no problem. But there is, but sometimes it just doesn't work. vcpkg installed, visio studio was not used to automatically find the relevant header files. Still prompted: can not open source file "xxx.h".

Some people say that re-executing vcpkg.exe integrate install will solve it? The answer is no, because vcpkg.exe integrate install vcpkg the third-party dependent function of visio studio, which means that as long as vcpkg installs a new package, visio studio will be updated immediately.

So if visio studio not updated immediately, it is mostly caused by our unsuccessful installation.

The criterion for judging whether a package is successfully installed is also very simple. We open the vcpkg folder and find the installed subfolder. If the current system is 64-bit, click x64-windows view; if it is 32-bit, click x86-windows view.

If there is a corresponding generated folder named after the third party, it means that vcpkg installed successfully, and the problem should be the link of visio studio integration.
image.png

If there is no corresponding folder, there is a problem with the installation. This problem is mostly caused by :x64-windows

If the version of VS we don’t care about is less than 2015, we may need with NuGet


潘杰
3.2k 声望245 粉丝