要应用第三方库,首先须要进行装置。在没有vcpkg
以前,装置第三方库须要在本地进行编译与链接,而如果咱们是老手,则必然会遇到这样或那样难以解决的问题。但有了vcpkg
当前就不一样,vcpkg
会主动的替咱们实现这所有。
装置vcpkg
以管理员权限运行powershell,执行进行clone我的项目、运行批处理文件、运行集成装置命令:
PS C:\Users\panjie\github> git clone https://github.com/microsoft/vcpkg --depth=1PS C:\Users\panjie\github> cd .\vcpkg\PS C:\Users\panjie\github\vcpkg> .\bootstrap-vcpkg.bat
稍等一会,待装置胜利的提醒。vcpkg
应该是vc++ package manager
的简称,即vc++包管理器
,它的作用同其它的包管理器。但可能是因为c++呈现的比拟早,所以其包管理器做的没有npm
这样的管理器那么优良。
装置第三方包
应用vcpkg
装置第三方包非常简单,只须要执行vcpkg install 包的名称
即可。如果咱们不晓得包的具体名称,还能够应用vcpkg search 包的名称
来进行搜寻,比方咱们想实现websocket
性能,则能够执行: .\vcpkg.exe search websocket
。
PS C:\github\vcpkg> .\vcpkg.exe search websocketbeast 0 HTTP/1 and WebSocket, header-only using Boost.Asio and C++11brynet 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 supportixwebsocket 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
或者也能够关上https://vcpkg.io/en/packages.html进行搜寻。
该页面给出了包的名称,版本号,次要性能形容以及兼容性。点击View Details
还会给出装置命令。
比方咱们此时在windows零碎中装置websocketpp
,按点击View Details
给出的提醒执行:.\vcpkg install websocketpp
如果你仅仅是为了学习,则能够尝试应用vcpkg install libzip
来装置一个较小的包:libzip
装置过程中的谬误,大多是因为网络起因造成的。装置的快与慢取决于计算机的CPU编译速度,咱们要做的仅仅是急躁期待。比方装置过程中停留在哪个界面,则须要查看vcpkg
是否在进行数据下载(查看网络状况),或是进行编译(查看CPU占用状况),如果两者都不是,则须要咱们按下回车来帮忙一下它。
vcpkg install
依赖于cmake
,如若以后计算机中未装置cmake
,vcpkg
将主动下载并装置。咱们所须要保障的是以后计算机的网络是畅通的。
装置胜利后vcpkg
将提醒如何在cmake
我的项目中配置以后包:
Elapsed time for package websocketpp:x86-windows: 10.13 sTotal elapsed time: 32.09 minThe package websocketpp:x86-windows provides CMake targets: find_package(websocketpp CONFIG REQUIRED) target_link_libraries(main PRIVATE websocketpp::websocketpp)
如果咱们用的是cmake
我的项目,则仅须要把上述两条命令复制下来,增加到CMakeLists.txt
中。
第三方包胜利装置并不是最终目标,咱们的最终目标是将第三方包加载到咱们的我的项目中。
至于我的项目有两类:第一类是应用visio studio开发的应用程序,该程序基于MSBuild
构建;第二类是应用其它工具开发的基于cmake
的应用程序。
Cmake
若要在cmake
中应用第三方包,就须要让cmake
可能胜利找到第三方包。vcpkg
应用了一种为cmake
增加参数的办法来实现这一性能。
执行vcpkg integrate install
后,将失去如下提示信息:
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"
比方在存在CMakeLists.txt
的门路上执行:cmake -DCMAKE_TOOLCHAIN_FILE=C:/Users/panjie/github/vcpkg/scripts/buildsystems/vcpkg.cmake
。
同时还能够为IDE增加编译参数,这样IDE在执行编译时便会主动退出上述参数,以Clion为例:
Visio Studio
若要在Visio Studio应用vcpkg
治理的第三方包,就须要让Visio Studio可能胜利的找到第三方包。因为vcpkg
也是微软本人的货色,所以应用起来也相当不便。只有咱们应用的Visio Studio的版本大于2015,便能够应用vcpkg.exe integrate install
来将vs与vcpkg联合在一起。
PS C:\Users\panjie\github\vcpkg> .\vcpkg.exe integrate installApplied 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....
须要留神的是:如果咱们在应用visio studio,则须要保障以后的VS中装置了英文的语言包。否则将失去如下正告信息:
Warning: The following VS instances are excluded because the English language pack is unavailable. C:\Program Files (x86)\Microsoft Visual Studio\2017\CommunityPlease install the English language pack.No suitable Visual Studio instances were found
以时则须要为vs装置英文语言包,而后从新执行:vcpkg integrate install
应用
安装包实现后,在我的项目中应用能够应用如下命令应用第三方包了:
#include <包名/头文件名.h>或#include <包名/头文件名.hpp>
比方:
#include <websocketpp/config/asio_no_tls.hpp>
如果你应用的IDE提醒没有找到相干头文件,则可能须要重新启动一下。