本文档翻译自:https://docs.conan.io/en/latest/getting_started.html#an-md5-hash-calculator-using-the-poco-libraries

    :::info Note
    创建这个项目的源文件可以在GitHub的示例存储库中找到。

    1. $ git clone https://github.com/conan-io/examples.git && cd examples/libraries/poco/md5

    :::

    1、在文件夹中创建以下源文件。这将是我们的应用程序的源文件:

    md5.cpp

    1. #include "Poco/MD5Engine.h"
    2. #include "Poco/DigestStream.h"
    3. #include <iostream>
    4. int main(int argc, char** argv)
    5. {
    6. Poco::MD5Engine md5;
    7. Poco::DigestOutputStream ds(md5);
    8. ds << "abcdefghijklmnopqrstuvwxyz";
    9. ds.close();
    10. std::cout << Poco::DigestEngine::digestToHex(md5.digest()) << std::endl;
    11. return 0;
    12. }

    2、我们知道我们的应用程序依赖于Poco库。让我们在 conan-center 中寻找它:

    1. $ conan search poco --remote=conan-center
    2. Existing package recipes:
    3. poco/1.8.1
    4. poco/1.9.3
    5. poco/1.9.4

    在搜索中必须指定 remote。否则,它将只搜索本地缓存。 :::info Note
    Conan客户端包含一个在远程存储库中搜索的命令,我们可以尝试 $ Conan search poco --remote=conan-center 您可以很好地使用此命令在您自己的存储库中搜索,但是请注意,此时在ConanCenter中可能会超时。基础设施也正在改进以支持此命令,但同时建议使用ConanCenter UI。 :::

    3、我们得到了关于 Poco 的数据。让我们检查一下1.9.4版本包的详细数据:

    1. $ conan inspect poco/1.9.4
    2. name: poco
    3. version: 1.9.4
    4. url: https://github.com/conan-io/conan-center-index
    5. homepage: https://pocoproject.org
    6. license: BSL-1.0
    7. author: None
    8. description: Modern, powerful open source C++ class libraries for building network- and internet-based applications that run on desktop, server, mobile and embedded systems.
    9. topics: ('conan', 'poco', 'building', 'networking', 'server', 'mobile', 'embedded')
    10. generators: cmake
    11. exports: None
    12. exports_sources: CMakeLists.txt
    13. short_paths: False
    14. apply_env: True
    15. build_policy: None
    16. revision_mode: hash
    17. settings: ('os', 'arch', 'compiler', 'build_type')
    18. options:
    19. cxx_14: [True, False]
    20. enable_apacheconnector: [True, False]
    21. enable_cppparser: [True, False]
    22. enable_crypto: [True, False]
    23. [...]
    24. default_options:
    25. cxx_14: False
    26. enable_apacheconnector: False
    27. enable_cppparser: False
    28. enable_crypto: True
    29. [...]

    4、接下来我们要指出我们的构建系统所需的依赖包(requires)和生成器(generators)。让我们在项目文件夹中创建一个conanfile.txt,内容如下:
    conanfile.txt

    1. [requires]
    2. poco/1.9.4
    3. [generators]
    4. cmake

    在本例中,我们使用 CMake 来构建项目,这就是为什么要指定 CMake 生成器。这个生成器创建一个conanbuildinfo.cmake 定义cmake变量的文件,包括可以在我们的构建中使用的路径和库名。阅读更多关于 Generators 的信息。

    5、下一步,我们将安装所需的依赖包,并生成构建的信息:

    1. $ mkdir build && cd build
    2. $ conan install ..
    3. ...
    4. Requirements
    5. openssl/1.0.2t from 'conan-center' - Downloaded
    6. poco/1.9.4 from 'conan-center' - Downloaded
    7. zlib/1.2.11 from 'conan-center' - Downloaded
    8. Packages
    9. openssl/1.0.2t:eb50d18a5a5d59bd0c332464a4c348ab65e353bf - Download
    10. poco/1.9.4:645aaff0a79e6036c77803601e44677556109dd9 - Download
    11. zlib/1.2.11:f74366f76f700cc6e991285892ad7a23c30e6d47 - Download
    12. zlib/1.2.11: Retrieving package f74366f76f700cc6e991285892ad7a23c30e6d47 from remote 'conan-center'
    13. Downloading conanmanifest.txt completed [0.25k]
    14. Downloading conaninfo.txt completed [0.44k]
    15. Downloading conan_package.tgz completed [83.15k]
    16. Decompressing conan_package.tgz completed [0.00k]
    17. zlib/1.2.11: Package installed f74366f76f700cc6e991285892ad7a23c30e6d47
    18. zlib/1.2.11: Downloaded package revision 0
    19. openssl/1.0.2t: Retrieving package eb50d18a5a5d59bd0c332464a4c348ab65e353bf from remote 'conan-center'
    20. Downloading conanmanifest.txt completed [4.92k]
    21. Downloading conaninfo.txt completed [1.28k]
    22. Downloading conan_package.tgz completed [3048.81k]
    23. Decompressing conan_package.tgz completed [0.00k]
    24. openssl/1.0.2t: Package installed eb50d18a5a5d59bd0c332464a4c348ab65e353bf
    25. openssl/1.0.2t: Downloaded package revision 0
    26. poco/1.9.4: Retrieving package 645aaff0a79e6036c77803601e44677556109dd9 from remote 'conan-center'
    27. Downloading conanmanifest.txt completed [48.75k]
    28. Downloading conaninfo.txt completed [2.44k]
    29. Downloading conan_package.tgz completed [5128.39k]
    30. Decompressing conan_package.tgz completed [0.00k]
    31. poco/1.9.4: Package installed 645aaff0a79e6036c77803601e44677556109dd9
    32. poco/1.9.4: Downloaded package revision 0
    33. conanfile.txt: Generator cmake created conanbuildinfo.cmake
    34. conanfile.txt: Generator txt created conanbuildinfo.txt
    35. conanfile.txt: Generated conaninfo.txt
    36. conanfile.txt: Generated graphinfo

    Conan安装了我们的Poco依赖关系,还安装了 Poco 的依赖包:OpenSSL 和 zlib。它还为我们的构建系统生成了conanbuildinfo.cmake文件。

    6、现在,我们来创建构建文件。要注入 Conan 信息,请包含生成的 conanbuildinfo.cmake 文件,如下所示:

    1. cmake_minimum_required(VERSION 2.8.12)
    2. project(MD5Encrypter)
    3. add_definitions("-std=c++11")
    4. include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
    5. conan_basic_setup()
    6. add_executable(md5 md5.cpp)
    7. target_link_libraries(md5 ${CONAN_LIBS})

    7、现在,我们准备构建并运行我们的Encrypter应用程序:

    1. (win)
    2. $ cmake .. -G "Visual Studio 16"
    3. $ cmake --build . --config Release
    4. (linux, mac)
    5. $ cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
    6. $ cmake --build .
    7. ...
    8. [100%] Built target md5
    9. $ ./bin/md5
    10. c3fcd3d76192e4007dfb496cca67e13b