CMake wrapper for the Conan C and C++ package manager.
This cmake module allows to launch conan install
from cmake.
The branches in this repo are:
You probably want to use a tagged release to ensure controlled upgrades.
You can just clone or grab the conan.cmake file and put in in your project.Or it can be used in this way. Note the v0.16.1
tag in the URL, change it to point to your desired release:
cmake_minimum_required(VERSION 3.5)
project(FormatOutput CXX)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
add_definitions("-std=c++11")
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/v0.16.1/conan.cmake"
"${CMAKE_BINARY_DIR}/conan.cmake"
EXPECTED_HASH SHA256=396e16d0f5eabdc6a14afddbcfff62a54a7ee75c6da23f32f7a31bc85db23484
TLS_VERIFY ON)
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)
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)
There are different functions you can use from your CMake project to use Conan from there. Therecommended flow to use cmake-conan is successively calling to conan_cmake_configure
,conan_cmake_autodetect
and conan_cmake_install
. This flow is recommended from v0.16 where thesefunctions were introduced.
The example above is using the Conan cmake_find_package
generator which is less intrusive than thecmake
generator and more aligned with the direction Conan is taking for the 2.0 version. If youwant to continue using the cmake
generator with conan_cmake_configure
, conan_cmake_autodetect
and conan_cmake_install
flow, you should manually include the conanbuildinfo.cmake
file generatedand also call to conan_basic_setup
:
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
Please check the cmake generator documentationfor further details.
This function will accept the same arguments as the sections of theconanfile.txt.
conan_cmake_configure(REQUIRES fmt/6.1.2
GENERATORS cmake_find_package
BUILD_REQUIRES cmake/3.15.7
IMPORTS "bin, *.dll -> ./bin"
IMPORTS "lib, *.dylib* -> ./bin")
OPTIONS fmt:shared=True)
This function will return the auto-detected settings (things like build_type, compiler or systemname) so you can pass that information to conan_cmake_install
. This step is optional as you maywant to rely on profiles, lockfiles or any other way of passing that information. This function willalso accept as arguments BUILD_TYPE
and ARCH
. Setting those arguments will force that settingsto the value provided (this can be useful for the multi-configuration generator scenario below).
conan_cmake_autodetect(settings)
This function is a wrapper for the conaninstall command. You canpass all the arguments that the command supports. Also, you can pass the auto-detected settings fromconan_cmake_autodetect
in the SETTINGS
argument.
It can receive as arguments: UPDATE
, NO_IMPORTS
, PATH_OR_REFERENCE
, REFERENCE
, REMOTE
,LOCKFILE
, LOCKFILE_OUT
, LOCKFILE_NODE_ID
, INSTALL_FOLDER
, GENERATOR
, BUILD
(if thisparameter takes the all
value, Conan will build everything from source), ENV
, ENV_HOST
,ENV_BUILD
, OPTIONS_HOST
, OPTIONS
, OPTIONS_BUILD
, PROFILE
, PROFILE_HOST
, PROFILE_BUILD
,SETTINGS
, SETTINGS_HOST
, SETTINGS_BUILD
. For more information, check conaninstall documentation.
It will also accept OUTPUT_QUIET
and ERROR_QUIET
arguments so that when it runs the conan install
command the output is quiet or the error is bypassed (or both).
conan_cmake_install(PATH_OR_REFERENCE .
BUILD missing
REMOTE conancenter
SETTINGS ${settings})
The recommended approach when using Multi Configuration generators like Visual Studio or Xcode islooping through the CMAKE_CONFIGURATION_TYPES
in your CMakeLists.txt and callingconan_cmake_autodetect
with the BUILD_TYPE
argument and conan_cmake_install
for each one usinga Conan multiconfig generator like cmake_find_package_multi
. Please check the example:
cmake_minimum_required(VERSION 3.5)
project(FormatOutput CXX)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
add_definitions("-std=c++11")
include(conan.cmake)
conan_cmake_configure(REQUIRES fmt/6.1.2 GENERATORS cmake_find_package_multi)
foreach(TYPE ${CMAKE_CONFIGURATION_TYPES})
conan_cmake_autodetect(settings BUILD_TYPE ${TYPE})
conan_cmake_install(PATH_OR_REFERENCE .
BUILD missing
REMOTE conancenter
SETTINGS ${settings})
endforeach()
find_package(fmt CONFIG)
add_executable(main main.cpp)
target_link_libraries(main fmt::fmt)
This function is not the recommended way of using cmake-conan any more and will be deprecated in thenear future. It will make the configure, auto-detect and install in one step so if you plan to useany new Conan features like lockfiles or build and host profiles it's possible that the auto-detectedsettings collide with the call to conan install.
conan_cmake_run(REQUIRES fmt/1.9.4
cgal/5.0.2
OPTIONS Pkg:shared=True
OtherPkg:option=value
)
Define requirements and their options. These values are written to a temporary conanfile.py
. If you need more advanced functionality, like conditional requirements, you can define your own conanfile.txt
or conanfile.py
and provideit with the CONANFILE
argument
If you want to use targets, you could do:
include(conan.cmake)
conan_cmake_run(REQUIRES fmt/1.9.4
BASIC_SETUP CMAKE_TARGETS
BUILD missing)
add_executable(main main.cpp)
target_link_libraries(main CONAN_PKG::fmt)
This will do a conan_basic_setup(TARGETS)
for modern CMake targets definition.
If you want to use your own conanfile.txt
or conanfile.py
instead of generating a temporary one, you could do:
include(conan.cmake)
conan_cmake_run(CONANFILE conanfile.txt # or relative build/conanfile.txt
BASIC_SETUP CMAKE_TARGETS
BUILD missing)
The resolution of the path will be relative to the root CMakeLists.txt
file.
conan_cmake_run(REQUIRES fmt/6.1.2 boost...
BASIC_SETUP
BUILD <value>)
Used to define the build policy used for conan install
. Can take different values:
BUILD all
. Build all the dependencies for the project.BUILD missing
. Build packages from source whose binary package is not found.BUILD outdated
. Build packages from source whose binary package was not generated from thelatest recipe or is not found.BUILD cascade
. Build packages from source that have at least one dependency being built fromsource.BUILD [pattern]
. Build packages from source whose package reference matches the pattern. Thepattern uses 'fnmatch' style wildcards.include(conan.cmake)
conan_cmake_run(CONANFILE conanfile.txt
BASIC_SETUP KEEP_RPATHS)
include(conan.cmake)
conan_cmake_run(CONANFILE conanfile.txt
BASIC_SETUP NO_OUTPUT_DIRS)
Pass to conan_basic_setup(NO_OUTPUT_DIRS)
so conanbuildinfo.cmake does not change the output directories (lib, bin).
include(conan.cmake)
conan_cmake_run(ARCH armv7)
Use it to override the architecture detection and force to call conan with the provided one. The architecture shouldexist in settings.yml.
include(conan.cmake)
conan_cmake_run(BUILD_TYPE "None")
Use it to override the build_type detection and force to call conan with the provided one. The build type shouldexist in settings.yml.
include(conan.cmake)
conan_cmake_run(CONFIGURATION_TYPES "Release;Debug;RelWithDebInfo")
Use it to set the different configurations when using multi-configuration generators. The defaultconfigurations used for multi-configuration generators are Debug
and Release
if the argumentCONFIGURATION_TYPES
is not specified The build types passed through this argument should existin settings.yml.
include(conan.cmake)
conan_cmake_run(PROFILE default)
Use it to use the "default" (or your own profile) conan profile rather than inferring settings from CMake.When it is defined, the CMake automatically detected settings are not used at all,and are overridden by the values from the profile.
include(conan.cmake)
conan_cmake_run(PROFILE default
PROFILE_AUTO build_type)
Use the CMake automatically detected value, instead of the profile one. The abovemeans use the profile named "default", but override its content with the build_type
automatically detected by CMake.
The precedence for settings definition is:
CMake detected < PROFILE < PROFILE_AUTO < Explicit ``conan_cmake_run()`` args
The ALL
value is used to use all detected settings from CMake, instead of the onesdefined in the profile:
include(conan.cmake)
conan_cmake_run(PROFILE default
PROFILE_AUTO ALL)
This is still useful, as the profile can have many other things defined (options, build_requires, etc).
To use the cmake_multi generator you just need to make sure CMAKE_BUILD_TYPE
is empty and use a CMake generator that supports multi-configuration.
If the BUILD_TYPE
is explictly passed to conan_cmake_run()
, then single configuration cmake
generator will be used.
include(conan.cmake)
conan_cmake_run(...
SETTINGS arch=armv6
SETTINGS compiler.cppstd=14)
include(conan.cmake)
conan_cmake_run(...
ENV env_var=value
ENV Pkg:env_var2=value2)
Define command line environment variables. Even if with CMake it is also possible todirectly define environment variables, with this syntax you can define environmentvariables per-package, as the above is equivalent to:
$ conan install .... -e env_var=value -e Pkg:env_var2=value
If environment variables were defined in a given profile, command line argumentshave higher precedence, so these values would be used instead of the profiles ones.
Provide the conan install --install-folder=[folder]
argument:
include(conan.cmake)
conan_cmake_run(...
INSTALL_FOLDER myfolder
)
Add additional generators. It may useful to add the virtualrunenv-generator:
include(conan.cmake)
conan_cmake_run(...
GENERATORS virtualrunenv)
List of files to be imported to a local folder. Read more about imports in Conan docs.
conan_cmake_run(...
IMPORTS "bin, *.dll -> ./bin"
IMPORTS "lib, *.dylib* -> ./bin")
Use NO_LOAD
argument to avoid loading the conanbuildinfo.cmake generated by the default cmake
generator.
include(conan.cmake)
conan_cmake_run(...
NO_LOAD)
Use CONAN_COMMAND
argument to specify the conan path, e.g. in case of running from source cmakedoes not identify conan as command, even if it is +x and it is in the path.
include(conan.cmake)
conan_cmake_run(...
CONAN_COMMAND "path_to_conan")
Checks conan availability in PATH.Arguments REQUIRED
and VERSION
are optional.
Example usage:
conan_check(VERSION 1.0.0 REQUIRED)
Adds a remote.Arguments URL
and NAME
are required, INDEX
and VERIFY_SSL
are optional.
Example usage:
conan_add_remote(NAME bincrafters
INDEX 1
URL https://api.bintray.com/conan/bincrafters/public-conan
VERIFY_SSL True)
Installs a full configuration from a local or remote zip file.Argument ITEM
is required, arguments TYPE
, SOURCE
, TARGET
and VERIFY_SSL
are optional.
Example usage:
conan_config_install(ITEM ./config.git TYPE git SOURCE src TARGET dst VERIFY_SSL False)
This cmake wrapper launches conan, installing dependencies, and injecting a conan_basic_setup()
call. So it is for end-users only, but not necessary at all for creating packages, because conan already downloaded and installed dependencies the moment that a package needs to be built. If you are using the same CMakeLists.txt for both consuming and creating packages, consider doing something like:
if(CONAN_EXPORTED) # in conan local cache
# standard conan installation, deps will be defined in conanfile.py
# and not necessary to call conan again, conan is already running
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
else() # in user space
include(conan.cmake)
# Make sure to use conanfile.py to define dependencies, to stay consistent
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})
endif()
Please check the source code for other options and arguments.
There are some tests, you can run in python, with pytest, for example:
$ pytest tests.py -rA
Cmake基本函数、变量等 add_library 指定源文件生成库文件 link_directories 将路径添加到库搜索路径中。使用TARGET_LINK_LIBRARIES时只需要给出库名字。(但不推荐 link_libraries 链接库,放在add_exexxx之前,使用路径直接链接库到所有目标 target_link_library ,放在add_exexxx之后,指定库名称(在li
场景: 在windows下跑slam代码,涉及到的库如Eigen,g2o等都是通过vcpkg安装的,用cmake 和mingw64直接对代码进行编译和生成可执行文件,即通过cmake … make(由mingw32-make.exe复制重命名make.exe) 命令来完成。 问题出现和解决 首先在cmake中要使用VCPKG,要在 CMakeLists.txt设置 SET(CMAKE_TOOLCH
我对CMake相当陌生,当我尝试编译时,我得到以下输出。 我整天都忙不过来-外面有人有经验吗? 对相关目录和文件的权限似乎很好。 任何帮助,不胜感激! 谢谢 The C compiler identification is GNU 4.6.2 The CXX compiler identification is GNU 4.6.2 Check for working C compiler: C:/
系列文章目录 SDL2 简明教程(一):使用 Cmake 和 Conan 构建 SDL2 编程环境 前言 最近在学习 FFMPEG,发现需要对 SDL 有所了解,因此在网上搜索一番后,发现一个还不错的教程:SDL2 TUTORIALS 。决定以这个教程为基础,翻译其内容,作为自己的学习纪录,同时方便其他新手更快的掌握 SDL2 的知识。 SDL2 TUTORIALS 中使用 Visual Stud
1. conan安装 1.1. 服务器安装 不要python2,用python3 >3.5 先安装pip:sudo apt install python3-pip 再安装conan:pip3 install conan PATH生效:$ source ~/.profile 如果出现:conan: command not found 执行链接:sudo ln -s ~/.local/bin/c
CMake Error at /usr/share/cmake-3.10/Modules/FindBoost.cmake:1947 (message): Unable to find the requested Boost libraries. Boost version: 1.65.1 Boost include path: /usr/include Could not f
【CMake】vcpkg + CMake 除了Conan外,vcpkg也是不错的C++包管理工具。跟conan相比,vcpkg是源码级的包管理,安装包的时候同时在本地编译。 安装vcpkg vcpkg支持众多架构,有arm-uwp, arm-windows, arm64-uwp, arm64-windows, x64-linux, x64-osx, x64-uwp, x64-windows, x6
Conan资源文档: 刚看了一点,大意就是:开发过程中,会用到一些外部包,要各种配置开发环境,包的版本发布管理等等问题,现在要解决它。 当前基于源码依赖的 开发模式 弊端: 对于外部库的依赖, 通常各个工程师在自己电脑安装, 坏处是 安装繁琐(有的要源码编译,有点命令安装, 不同人装的可能是不同版本号) 对于内部依赖, 当前都是通过git源码依赖, git submodule管理. 带来两个问题:
CMake 是一个跨平台的自动化构建系统,它使用一个名为 CMakeLists.txt 的文件来描述构建过程,可以产生标准的构建文件,如 Unix 的 Makefile 或Windows Visual C++ 的 projects/workspaces 。文件 CMakeLists.txt 需要手工编写,也可以通过编写脚本进行半自动的生成。CMake 提供了比 autoconfig 更简洁的语法。
?> Hello world,世界 你好 本节选择了一个最简单的例子 Helloworld 来演练一下 cmake 的完整构建过程,本节并不会深入的探讨 cmake,仅仅展示一个简单的例子,并加以粗略的解释。我们选择了Everest Linux 作为基本开发平台,因为这个只有一张 CD 的发行版本,包含了 gcc4.2/gtk/qt3/qt4等完整的开发环境,同时,系统默认集成了 cmake 最新
CMake Cookbook This repository collects sources for the recipes contained in theCMake Cookbookpublished by Packt and authored by Radovan Bast andRoberto Di Remigio Contributing Testing Table of conten
learning-cmake This is a simple CMake tutorial project which contains some different scenarios. hello-world: Demo a simplest CMake project. hello-world-clear: Separate the output files and src files.
switch-cmake Extensible CMake toolchain for Switch homebrew development with devkitA64 and libnx. Table of Contents Introduction Why CMake Quick Start Switch Homebrew File Formats devkitPro Ecosystem
A CMake toolchain file for iOS (+ Catalyst), watchOS, tvOS and macOS development with full simulator support and toggleable options! NEW! Experimental Catalyst support (iOS on macOS) macOS support and