同步链接地址:https://yangshun.win/blogs/3b103680/
Windows 下使用 Vcpkg 配置百度 AI 图像识别 C++开发环境(VS2017)
本机环境
- Windows 10 专业版
- Visual Studio Community 2017 (版本 15.9.7)
Windows 下配置 Vcpkg
Vcpkg 是适用于 Windows,Linux 和 MacOS 的 C++ 库管理器,使用它可以方便地管理 C++ 的依赖库。
Vcpkg 的下载地址和使用说明: https://github.com/microsoft/vcpkg
To get started:
> git clone https://github.com/Microsoft/vcpkg.git > cd vcpkg PS> .\bootstrap-vcpkg.bat Linux:~/$ ./bootstrap-vcpkg.sh
Then, to hook up user-wide integration, run (note: requires admin on first use)
PS> .\vcpkg integrate install Linux:~/$ ./vcpkg integrate install
百度 AI C++ 版本的 SDK 代码中主要使用了依赖库 curl (需要支持 ssl) openssl jsoncpp ( >1.6.2 版本,0.x 版本将不被支持)。因此直接使用 Vcpkg 来安装这些依赖库。
如果不指定安装的架构,vcpkg 默认把开源库编译成 x86 的 Windows 版本的库。可以使用一下代码查询对应的版本:
$ .\vcpkg.exe help triplet Available architecture triplets: arm-uwp arm-windows arm64-uwp arm64-windows x64-linux x64-osx x64-uwp x64-windows x64-windows-static x86-uwp x86-windows x86-windows-static
我这里编译的版本为 x64,因此使用 x64-window。
> cd d:\vcpkg # 根据你的目录进行修改 > ./vcpkg.exe install curl:x64-windows > ./vcpkg.exe install jsoncpp:x64-windows > ./vcpkg.exe install openssl:x64-windows
每安装完一个库,都会提示如何包含库,执行上面代码后会输出以下使用说明:
find_package(CURL CONFIG REQUIRED) target_link_libraries(main PRIVATE CURL::libcurl) find_package(jsoncpp CONFIG REQUIRED) target_link_libraries(main PRIVATE jsoncpp_lib) find_package(OpenSSL REQUIRED) target_link_libraries(main PRIVATE OpenSSL::SSL OpenSSL::Crypto)
安装的各种库的版本:
$ ./vcpkg.exe list curl:x64-windows 7.66.0 A library for transferring data with URLs curl[ssl]:x64-windows Default SSL backend curl[winssl]:x64-windows SSL support (Secure Channel / "WinSSL") jsoncpp:x64-windows 1.9.1 jsoncpp is an implementation of a JSON reader an... openssl-windows:x64-windows 1.0.2s-1 OpenSSL is an open source project that provides ... openssl:x64-windows 1 OpenSSL is an open source project that provides ... zlib:x64-windows 1.2.11-5 A compression library
配置 Visual Studio 使用 Vcpkg 安装的库
集成到全局
Vcpkg 提供了一套机制,可以全自动的适配目录,而开发者不需要关心已安装的库的目录在哪里,也不需要设置。
$ ./vcpkg.exe integrate install Applied user-wide integration for this vcpkg root. All MSBuild C++ projects can now #include any installed libraries. Linking will be handled automatically. Installing new libraries will make them instantly available. CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=D:/vcpkg/scripts/buildsystems/vcpkg.cmake"
当出现 “Applied user-wide integration for this vcpkg root.” 字样的时候,说明已经集成成功。这时候可以在任意的工程中使用安装好的第三方库。
移除全局集成:
./vcpkg.exe integrate remove
集成到工程
"集成到工程”需要利用 Visual Studio 中的 nuget 插件来实现。
生成配置
执行命令
$ ./vcpkg.exe integrate project Created nupkg: D:\vcpkg\scripts\buildsystems\vcpkg.D.vcpkg.1.0.0.nupkg With a project open, go to Tools->NuGet Package Manager->Package Manager Console and paste: Install-Package vcpkg.D.vcpkg -Source "D:\vcpkg\scripts\buildsystems"
执行命令成功后会在 “\scripts\buildsystems” 目录下,生成 nuget 配置文件.
NuGet配置
在 Visual Studio 中,点击菜单 “工具->选项”, 选择"NuGet包管理器->程序包源".
添加新的可用程序包源, 选择 vcpkg 目录下的 “scripts\buildsystems” 目录,然后点击右侧的 “更新” 按钮。
点击 “确定” 按钮,关闭对话框。
到此,全局性的设置已经完成。
工程配置
用 Visual Studio 打开一个工程或解决方案。右键点击需要设置的工程弹出菜单,选择“管理 NuGet 程序包”。
在右上角的 “程序包源” 中选择刚刚设置的 “vcpkg”。这样在 “浏览” 选项卡中就可以看到 “vcpkg.D.vcpkg”。点击最右侧的 “安装”。这样就可以集成到某个工程了。
测试 Jsoncpp 库
这里使用的是 jsoncpp 官方的 example。
#include #include int main() { const std::string rawJson = R"({"Age": 20, "Name": "colin"})"; const int rawJsonLength = static_cast(rawJson.length()); JSONCPP_STRING err; Json::Value root; Json::CharReaderBuilder builder; const std::unique_ptr reader(builder.newCharReader()); if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root, &err)) { std::cout << "error" << std::endl; return EXIT_FAILURE; } const std::string name = root["Name"].asString(); const int age = root["Age"].asInt(); std::cout << name << std::endl; std::cout << age << std::endl; system("pause"); return EXIT_SUCCESS; }
如果能正常编译并输出结果则表示库安装成功了。
get,学习一下~