官方文档:Getting Started

测试环境:Windows10、Visual Studio 2017(VC++15)

编写代码

此代码使用了Poco包

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

使用conan安装Poco包

conan的安装

一、安装Python3
Conan基于Python编写,所以需要安装Python

二、安装Conan
使用Python的包管理工具pip安装Conan

  1. # Windows
  2. pip install conan
  3. # Linux需要使用sudo安装全局的Python包
  4. sudo pip install conan
  5. # OSX下也可以使用brew
  6. brew update
  7. brew install conan

三、修改包存放路径
找到文件:C:\Users\Administrator.conan\conan.conf
修改路径

  1. [storage]
  2. path = D:\code\conan_data

管理项目的依赖库(编写conanfile文件)

使用命令搜索Poco,查看服务器中有哪些版本

  1. C:\Users\2107>conan search poco --remote=conancenter
  2. Existing package recipes:
  3. poco/1.8.1
  4. poco/1.9.3
  5. poco/1.9.4
  6. poco/1.10.0
  7. poco/1.10.1
  8. poco/1.11.0
  9. poco/1.11.1

查看包的详细说明

  1. C:\Users\2107>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: ('building', 'networking', 'server', 'mobile', 'embedded')
  10. generators: ('cmake', 'cmake_find_package')
  11. exports: None
  12. exports_sources: None
  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. enable_active_record: [True, False]
  20. enable_apacheconnector: [True, False]
  21. enable_cppparser: [True, False]
  22. enable_crypto: [True, False]
  23. enable_data: [True, False]
  24. enable_data_mysql: [True, False]
  25. enable_data_odbc: [True, False]
  26. enable_data_postgresql: [True, False]
  27. enable_data_sqlite: [True, False]
  28. enable_encodings: [True, False]
  29. enable_fork: [True, False]
  30. enable_json: [True, False]
  31. enable_jwt: [True, False]
  32. enable_mongodb: [True, False]
  33. enable_net: [True, False]
  34. enable_netssl: [True, False]
  35. enable_netssl_win: [True, False]
  36. enable_pagecompiler: [True, False]
  37. enable_pagecompiler_file2page: [True, False]
  38. enable_pdf: [True, False]
  39. enable_pocodoc: [True, False]
  40. enable_redis: [True, False]
  41. enable_sevenzip: [True, False]
  42. enable_util: [True, False]
  43. enable_xml: [True, False]
  44. enable_zip: [True, False]
  45. fPIC: [True, False]
  46. shared: [True, False]
  47. default_options:
  48. enable_active_record: True
  49. enable_apacheconnector: False
  50. enable_cppparser: False
  51. enable_crypto: True
  52. enable_data: True
  53. enable_data_mysql: True
  54. enable_data_odbc: False
  55. enable_data_postgresql: True
  56. enable_data_sqlite: True
  57. enable_encodings: True
  58. enable_fork: True
  59. enable_json: True
  60. enable_jwt: True
  61. enable_mongodb: True
  62. enable_net: True
  63. enable_netssl: True
  64. enable_netssl_win: False
  65. enable_pagecompiler: False
  66. enable_pagecompiler_file2page: False
  67. enable_pdf: False
  68. enable_pocodoc: False
  69. enable_redis: True
  70. enable_sevenzip: False
  71. enable_util: True
  72. enable_xml: True
  73. enable_zip: True
  74. fPIC: True
  75. shared: False
  76. deprecated: Non

确定使用poco/1.9.4之后,就可以在项目目录下创建conanfile.txt文件,编写此项目的conan包依赖

  • 注:使用cmake工程构建器

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

安装依赖包并构建系统生成信息

  1. # 在项目目录中创建一个build目录,并进入此目录
  2. $ mkdir build && cd build
  3. # 根据conanfile.txt安装依赖包,生成的文件放到此目录下(./build)
  4. $ conan install .. -settings compiler.version=15
  5. #.. 代表conanfile.txt所在目录,即上一级目录
  6. #指定编译器为VC++15(即Visual Studio17)

可以看到,conan为我们安装了Poco和Poco所依赖的库。

具体原理如下:

  1. 在conan server中,每个包中,都有一个叫做recipe的东西(蓝色方块),并且有不同配置信息的、已经编译好的包
  2. 在客户端要使用时,会去服务器上,拿去对应配置信息的二进制包

image.png

注:如果conan install ..后面没有指定profile(参数)文件的话,使用的是C:\Users\2107\.conan\profiles\default

  • 它(default)会在你第一次使用时,生成一个默认的
  • 值得注意的是,如果你电脑上有多个VS版本(如VS17、VS19、VS2022),它会默认使用最新的版本(即VS2022)。但不幸的是,VS2022(VC++17编译器)的预编译版本还很少,所以不建议使用。你可以把compiler.version=17改成compiler.version=15(即VS17)的版本

编写cmake文件

  1. cmake_minimum_required(VERSION 2.8.12)
  2. project(MD5Encrypter)
  3. add_definitions("-std=c++11")
  4. #conan install之后,会生成一个conanbuildinfo.cmake文件,以方便我们的cmake找到conan安装的包
  5. #根据上面的命令,conanbuildinfo.cmake文件生成在./build目录下
  6. #注:在cmake构建工程时,将${CMAKE_BINARY_DIR}指定为/build目录
  7. include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
  8. conan_basic_setup() #conan初始设置
  9. add_executable(md5 md5.cpp)
  10. target_link_libraries(md5 ${CONAN_LIBS}) #链接你使用conan安装的所有lib文件

编译

  1. # 使用cmake构建VS工程
  2. cmake .. -G "Visual Studio 15 Win64"
  3. # Visual Studio 15指定是VC++15,对应的就是Visual Studio2017
  4. # Win64,代表构建x64版本
  5. # 编译
  6. cmake --build . --config Release
  7. #编译结果:build/bin/md5.exe
  8. # 运行结果
  9. cd bin
  10. md5.exe

错误合集

conan install时,未找到库

conan install ..之后,可能会报以下错误,意思是服务器上没有事先编译好的。

  1. bzip2/1.0.8: WARN: Can't find a 'bzip2/1.0.8' package for the specified settings, options and dependencies:
  2. - Settings: arch=x86_64, build_type=Release, compiler=Visual Studio, compiler.runtime=MD, compiler.version=17, os=Windows
  3. - Options: build_executable=True, shared=False
  4. - Dependencies:
  5. - Requirements:
  6. - Package ID: 53b5fd22ba061620078deefdae9a437c5f693201
  7. ERROR: Missing prebuilt package for 'bzip2/1.0.8', 'expat/2.4.1', 'openssl/1.1.1l', 'pcre/8.45', 'poco/1.9.4', 'sqlite3/3.36.0', 'zlib/1.2.11'
  8. Use 'conan search bzip2/1.0.8 --table=table.html -r=remote' and open the table.html file to see available packages
  9. Or try to build locally from sources with '--build=missing'
  10. More Info at 'https://docs.conan.io/en/latest/faq/troubleshooting.html#error-missing-prebuilt-package

如果遇到没有事先编译好的,conan服务器可以帮你拿着源码+你配置的参数(arch、build_type、compiler等等)帮你现场编译。你只需在安装命令后加上--build=missing,就能触发编译的操作。

但是经常会编译失败。这可能是因为它的编译脚本和我们的编译环境不适配有关。

  1. bzip2/1.0.8:
  2. bzip2/1.0.8: ERROR: Package '53b5fd22ba061620078deefdae9a437c5f693201' build failed
  3. bzip2/1.0.8: WARN: Build folder C:\Users\2107\.conan\data\bzip2\1.0.8\_\_\build\53b5fd22ba061620078deefdae9a437c5f693201
  4. ERROR: bzip2/1.0.8: Error in build() method, line 67
  5. cmake = self._configure_cmake()
  6. while calling '_configure_cmake', line 61
  7. self._cmake.configure()
  8. ConanException: Error 1 while executing cd C:\Users\2107\.conan\data\bzip2\1.0.8\_\_\build\53b5fd22ba061620078deefdae9a437c5f693201 && cmake -G "Visual Studio 17 2022" -A "x64" -DCONAN_LINK_RUNTIME="/MD" -DCONAN_IN_LOCAL_CACHE="ON" -DCONAN_COMPILER="Visual Studio" -DCONAN_COMPILER_VERSION="17" -DCONAN_CXX_FLAGS="/MP12" -DCONAN_C_FLAGS="/MP12" -DBUILD_SHARED_LIBS="OFF" -DCMAKE_INSTALL_PREFIX="C:\Users\2107\.conan\data\bzip2\1.0.8\_\_\package\53b5fd22ba061620078deefdae9a437c5f693201" -DCMAKE_INSTALL_BINDIR="bin" -DCMAKE_INSTALL_SBINDIR="bin" -DCMAKE_INSTALL_LIBEXECDIR="bin" -DCMAKE_INSTALL_LIBDIR="lib" -DCMAKE_INSTALL_INCLUDEDIR="include" -DCMAKE_INSTALL_OLDINCLUDEDIR="include" -DCMAKE_INSTALL_DATAROOTDIR="share" -DCMAKE_EXPORT_NO_PACKAGE_REGISTRY="ON" -DCONAN_EXPORTED="1" -DBZ2_VERSION_STRING="1.0.8" -DBZ2_VERSION_MAJOR="1" -DBZ2_BUILD_EXE="True" -Wno-dev C:\Users\2107\.conan\data\bzip2\1.0.8\_\_\build\53b5fd22ba061620078deefdae9a437c5f693201

解决方式(解决不了,曲线救国)

  • 去看服务器有哪些预编译的版本,然后换编译器(使用Linux环境,这种问题就少)
  • 搭建私有的conan服务器,编译好,传上去

附:生成器版本

  1. Generators
  2. * Visual Studio 16 2019 = Generates Visual Studio 2019 project files.
  3. Use -A option to specify architecture.
  4. Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
  5. Optional [arch] can be "Win64" or "ARM".
  6. Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
  7. Optional [arch] can be "Win64" or "ARM".
  8. Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
  9. Optional [arch] can be "Win64" or "ARM".
  10. Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
  11. Optional [arch] can be "Win64" or "ARM".
  12. Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files.
  13. Optional [arch] can be "Win64" or "IA64".
  14. Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files.
  15. Optional [arch] can be "Win64" or "IA64".
  16. Borland Makefiles = Generates Borland makefiles.
  17. NMake Makefiles = Generates NMake makefiles.
  18. NMake Makefiles JOM = Generates JOM makefiles.
  19. MSYS Makefiles = Generates MSYS makefiles.
  20. MinGW Makefiles = Generates a make file for use with
  21. mingw32-make.
  22. Unix Makefiles = Generates standard UNIX makefiles.
  23. Green Hills MULTI = Generates Green Hills MULTI files
  24. (experimental, work-in-progress).
  25. Ninja = Generates build.ninja files.
  26. Ninja Multi-Config = Generates build-<Config>.ninja files.
  27. Watcom WMake = Generates Watcom WMake makefiles.
  28. CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files.
  29. CodeBlocks - NMake Makefiles = Generates CodeBlocks project files.
  30. CodeBlocks - NMake Makefiles JOM
  31. = Generates CodeBlocks project files.
  32. CodeBlocks - Ninja = Generates CodeBlocks project files.
  33. CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
  34. CodeLite - MinGW Makefiles = Generates CodeLite project files.
  35. CodeLite - NMake Makefiles = Generates CodeLite project files.
  36. CodeLite - Ninja = Generates CodeLite project files.
  37. CodeLite - Unix Makefiles = Generates CodeLite project files.
  38. Sublime Text 2 - MinGW Makefiles
  39. = Generates Sublime Text 2 project files.
  40. Sublime Text 2 - NMake Makefiles
  41. = Generates Sublime Text 2 project files.
  42. Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
  43. Sublime Text 2 - Unix Makefiles
  44. = Generates Sublime Text 2 project files.
  45. Kate - MinGW Makefiles = Generates Kate project files.
  46. Kate - NMake Makefiles = Generates Kate project files.
  47. Kate - Ninja = Generates Kate project files.
  48. Kate - Unix Makefiles = Generates Kate project files.
  49. Eclipse CDT4 - NMake Makefiles
  50. = Generates Eclipse CDT 4.0 project files.
  51. Eclipse CDT4 - MinGW Makefiles
  52. = Generates Eclipse CDT 4.0 project files.
  53. Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
  54. Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.