cmake-conan源代码:https://github.com/conan-io/cmake-conan
CMake-conan帮助我们在CMake中使用Conan,而不用手动执行conan install
命令。
你可以直接把本仓库中的conan.cmake
文件放到项目当中,也可以通过网络下载。
cmake_minimum_required(VERSION 3.9)
project(FormatOutput CXX)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
add_definitions("-std=c++11")
#
# 在cmake当中使用CMake
#
#如果Bin目录下不存在conan.cmake文件,则从远端下载
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/0.18.1/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake"
TLS_VERIFY ON)
endif()
#包含conan.cmake文件
include(${CMAKE_BINARY_DIR}/conan.cmake)
#CMake配置(即对应conanfile.txt文件)
conan_cmake_configure(REQUIRES fmt/6.1.2
GENERATORS cmake_find_package)
conan_cmake_autodetect(settings)
#安装命令
conan_cmake_install(PATH_OR_REFERENCE .
BUILD missing
REMOTE conancenter
SETTINGS ${settings})
#
# 使用包
#
find_package(fmt)
add_executable(main main.cpp)
target_link_libraries(main fmt::fmt)
conan_cmake_configure()
此方法接收的参数与conanfile.txt文件内容相似。
conan_cmake_configure(REQUIRES fmt/6.1.2
cgal/5.0.2
GENERATORS cmake_find_package
BUILD_REQUIRES cmake/3.15.7
IMPORTS "bin, *.dll -> ./bin"
IMPORTS "lib, *.dylib* -> ./bin")
OPTIONS fmt:shared=True
Pkg:shared=True
OtherPkg:option=value)
conan_cmake_install()
此方法是conan install
命令的包装器。你可以传递该命令支持的所有参数。
参数可看:https://docs.conan.io/en/latest/reference/commands/consumer/install.html
conan_cmake_install(PATH_OR_REFERENCE .
BUILD missing
REMOTE conancenter
SETTINGS ${settings})
conan_cmake_autodetect()
这个方法会自动检测(auto-detected)项目的配置(如build_type
、compiler
和系统名称)
conan_cmake_autodetect(settings)
所以你可以把settings
传给conan_cmake_install()
conan_cmake_install(PATH_OR_REFERENCE .
BUILD missing
REMOTE conancenter
SETTINGS ${settings})
如果你想使用系统当中的profile配置,这个方法是可选的,不需要调用。