zoukankan      html  css  js  c++  java
  • (基础篇 02)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

    如果想要了解 Vcpkg,可以参考一下 这篇原创博客

    To get started:

    > git clone https://github.com/Microsoft/vcpkg.git
    > cd vcpkg
    
    PS> .ootstrap-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
    

    Install any packages with

    PS> .vcpkg install sdl2 curl
    Linux:~/$ ./vcpkg install sdl2 curl
    

    百度 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:vcpkgscriptsuildsystemsvcpkg.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:vcpkgscriptsuildsystems"
    
    

    执行命令成功后会在 “scriptsuildsystems” 目录下,生成 nuget 配置文件.

    NuGet配置

    在 Visual Studio 中,点击菜单 “工具->选项”, 选择"NuGet包管理器->程序包源".

    添加新的可用程序包源, 选择 vcpkg 目录下的 “scriptsuildsystems” 目录,然后点击右侧的 “更新” 按钮。

    点击 “确定” 按钮,关闭对话框。

    到此,全局性的设置已经完成。

    在这里插入图片描述

    工程配置

    用 Visual Studio 打开一个工程或解决方案。右键点击需要设置的工程弹出菜单,选择“管理 NuGet 程序包”。

    在这里插入图片描述

    在右上角的 “程序包源” 中选择刚刚设置的 “vcpkg”。这样在 “浏览” 选项卡中就可以看到 “vcpkg.D.vcpkg”。点击最右侧的 “安装”。这样就可以集成到某个工程了。

    在这里插入图片描述

    测试 Jsoncpp 库

    这里使用的是 jsoncpp 官方的 example。

    #include <iostream>
    #include <json/json.h>
    
    int main() {
    	const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
    	const int rawJsonLength = static_cast<int>(rawJson.length());
    	JSONCPP_STRING err;
    	Json::Value root;
    
    	Json::CharReaderBuilder builder;
    	const std::unique_ptr<Json::CharReader> 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;
    }
    

    如果能正常编译并输出结果则表示库安装成功了。

    参考链接

  • 相关阅读:
    运维实战:两台服务器http方式共享yum软件仓库
    初始化thinkphp6.0出现的问题解决
    记一次续签SSL证书导致微信小程序部分机型无法访问网站接口
    微信小程序-订阅消息验证发送值有效格式
    微信小程序分包优化
    MySQL timeout 参数详解
    mysql 事件
    springboot 远程拉取配置中心配置
    使用springboot的resttmplate请求远程服务的时候报 403问题
    for 循环 与增强的for循环 经验小结
  • 原文地址:https://www.cnblogs.com/busyboxs/p/12245410.html
Copyright © 2011-2022 走看看