cmake-conan源代码:https://github.com/conan-io/cmake-conan

CMake-conan帮助我们在CMake中使用Conan,而不用手动执行conan install命令。

你可以直接把本仓库中的conan.cmake文件放到项目当中,也可以通过网络下载。

  1. cmake_minimum_required(VERSION 3.9)
  2. project(FormatOutput CXX)
  3. list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
  4. list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
  5. add_definitions("-std=c++11")
  6. #
  7. # 在cmake当中使用CMake
  8. #
  9. #如果Bin目录下不存在conan.cmake文件,则从远端下载
  10. if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
  11. message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
  12. file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/0.18.1/conan.cmake"
  13. "${CMAKE_BINARY_DIR}/conan.cmake"
  14. TLS_VERIFY ON)
  15. endif()
  16. #包含conan.cmake文件
  17. include(${CMAKE_BINARY_DIR}/conan.cmake)
  18. #CMake配置(即对应conanfile.txt文件)
  19. conan_cmake_configure(REQUIRES fmt/6.1.2
  20. GENERATORS cmake_find_package)
  21. conan_cmake_autodetect(settings)
  22. #安装命令
  23. conan_cmake_install(PATH_OR_REFERENCE .
  24. BUILD missing
  25. REMOTE conancenter
  26. SETTINGS ${settings})
  27. #
  28. # 使用包
  29. #
  30. find_package(fmt)
  31. add_executable(main main.cpp)
  32. target_link_libraries(main fmt::fmt)

conan_cmake_configure()

此方法接收的参数与conanfile.txt文件内容相似。

  1. conan_cmake_configure(REQUIRES fmt/6.1.2
  2. cgal/5.0.2
  3. GENERATORS cmake_find_package
  4. BUILD_REQUIRES cmake/3.15.7
  5. IMPORTS "bin, *.dll -> ./bin"
  6. IMPORTS "lib, *.dylib* -> ./bin")
  7. OPTIONS fmt:shared=True
  8. Pkg:shared=True
  9. OtherPkg:option=value)

conan_cmake_install()

此方法是conan install命令的包装器。你可以传递该命令支持的所有参数。
参数可看:https://docs.conan.io/en/latest/reference/commands/consumer/install.html

  1. conan_cmake_install(PATH_OR_REFERENCE .
  2. BUILD missing
  3. REMOTE conancenter
  4. SETTINGS ${settings})

conan_cmake_autodetect()

这个方法会自动检测(auto-detected)项目的配置(如build_typecompiler和系统名称)

  1. conan_cmake_autodetect(settings)

所以你可以把settings传给conan_cmake_install()

  1. conan_cmake_install(PATH_OR_REFERENCE .
  2. BUILD missing
  3. REMOTE conancenter
  4. SETTINGS ${settings})

如果你想使用系统当中的profile配置,这个方法是可选的,不需要调用。