Cheat Sheet

快捷命令

  1. 初始化

    1
    
    conan install . -if build && cmake -S . -B build -G Ninja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
  2. 编译

    1
    
    cmake --build build

搜索

本地搜索

conan search <pattern, eg: boost>

远程搜索

conan search -r all boost
conan search -r conan-center boost

包详情查看

conan inspect boost/1.76.0

修改 profile

conan profile update settings.compiler.libcxx=libstdc++11 default

快速用法

  1. 在 conancenter 中 https://conan.io/center 查找要使用的包
  2. 在结果页中,打开选项卡 Use it 查看使用方法
  3. 例子:

验证流程

linux

  • conan 版本: v1.49.0
  • 安装包: boost/v1.79.0
  • CMakeLists.txt 文件

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    cmake_minimum_required(VERSION 3.15)
    project(main CXX)
    
    # conan 配置
    include(${CMAKE_BINARY_DIR}/conan_paths.cmake)
    
    find_package(Boost)
    
    message("find boost: " ${Boost_FOUND} ${Boost_DIR} ${BOOST_INCLUDE_DIRS})
    
    
    add_executable(${PROJECT_NAME} hello.cpp)
    
    # 老方法
    if(Boost_FOUND)
      include_directories(${Boost_INCLUDE_DIRS})
      target_link_libraries(main ${Boost_LIBRARIES})
    endif()
    
    # 新方法
    if(TARGET Boost::Boost)
      target_link_libraries(main Boost::Boost)
    endif()
  • conanfile.txt 文件

    1
    2
    3
    4
    5
    6
    
    [requires]
    boost/1.79.0
    
    [generators]
    cmake_find_package
    cmake_paths

conan + cmake 命令流程:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
conan install --install-folder build . --build missing

# 注意: 不指定 build_type 报错, 在 windows 上没错误
cmake -S . -B build \
      -DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake \
      -DCMAKE_BUILD_TYPE=Debug

# 编译
cmake --build build

# bear ---> for emacs
bear -- cmake --build build

Win 10

  1. conanfile.txt

    1
    2
    3
    4
    5
    6
    
    [requires]
    boost/1.79.0
    
    [generators]
    cmake_find_package
    cmake_paths
  2. CMakeLists.txt

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    
    cmake_minimum_required(VERSION 3.15)
    project(main CXX)
    set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # 用来生成 compile_commands.json
    
    # conan 配置
    include(${CMAKE_BINARY_DIR}/conan_paths.cmake)
    
    find_package(Boost)
    
    message("find boost: " ${Boost_FOUND} ${Boost_DIR} ${BOOST_INCLUDE_DIRS})
    
    
    add_executable(${PROJECT_NAME} hello.cpp)
    
    
    # 老方法
    if(Boost_FOUND)
      include_directories(${Boost_INCLUDE_DIRS})
      target_link_libraries(main ${Boost_LIBRARIES})
    endif()
    
    # 新方法
    if(TARGET Boost::Boost)
      target_link_libraries(main Boost::Boost)
    endif()
  3. conan 安装依赖

    1
    
    conan install . -if build --build missing
  4. cmake 初始化

    1
    
    cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -G Ninja
  5. lsp 支持

    1
    
    cp .\build\compile_commands.json .\

generators

参考:

cmake

  • 作用

    • 在 conan 中完成依赖管理

      • libraries 链接库依赖
      • headers 头文件依赖
  • 实现

    • conanfile.txt

      1
      2
      3
      4
      5
      
      [requires]
      boost/1.76.0
      
      [generators]
      cmake
    • CMakeLists.txt

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      
      CMAKE_MINIMUM_REQUIRED(VERSION 3.0)
      
      project(Hello_Demo)
      
      
      # conan 配置
      # load conanbuildinfo.cmake
      include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
      # 老方法------------------------
      conan_basic_setup()
      
      
      add_executable(main main.cpp)
      target_link_libraries(main ${CONAN_LIBS})
      # --------------------------------------------
      
      
      # 新方法 modern cmake(>3.1.2) ------------------
      conan_basic_setup(TARGETS)
      
      add_executable(main main.cpp)
      target_link_libraries(main Boost::Boost)
      # ---------------------------------------------

cmake_find_package

  • 注意

    • cmake_paths generator 一起使用

和 cmake_paths 一起使用

  • 在 cmake 中调用

    1. conan install ..
    2. cmake ..
  • conanfile.txt

    1
    2
    3
    4
    5
    6
    
    [requires]
    boost/1.72.0
    
    [generators]
    cmake_find_package
    cmake_paths
  • CMakeLists.txt

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    cmake_mininum_required(VERSION 3.0)
    
    project(My_Project)
    
    # load cmake_paths.cmake
    include(${CMAKE_BINARY_DIR}/cmake_paths.cmake)
    
    add_executable(hello hello.cpp)
    
    find_package(Boost)
    
    # 老方法
    if(Boost_FOUND)
      include(${Boost_INCLUDE_DIRS})
      target_link_libraries(hello ${Boost_LIBRARIES})
    endif()
    
    # 新方法
    if(TARGET Boost::Boost)
      target_link_libraries(hello Boost::Boost)
    endif()

cmake_find_package 单独使用

  • 不能直接 cmake ..
  • 而要

    1. conan create
cmake_find_package 单独使用,直接 cmake ..
  • 修改 CMakeLists.txt

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    CMAKE_MINIMUM_REQUIRED(VERSION 3.0)
    
    project(My_DEMO)
    
    list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
    list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
    
    add_executable(main main.cpp)
    
    
    find_package(Boost REQUIRED)
    
    if(TARGET Boost::Boost)
      target_link_libraries(main Boost::Boost)
    endif()

cmake_paths

动态链接库 dynamic link library

配置方法: 使用 options

demo:

1
2
3
4
5
6
7
8
9
[requires]
poco/1.9.4

[generators]
cmake

[options]
poco:shared=True # PACKAGE:OPTION=VALUE
openssl:shared=True

通过命令行指定

1
2
3
$ conan install .. -o poco:shared=True -o openssl:shared=True
# or even with wildcards, to apply to many packages
$ conan install .. -o *:shared=True

conan + cmake 找到的链接库名称

  1. Boost 为例:

    • CONAN_LIB::Boost_boost_unit_test_framework
    • 注意:

      • 测试: Boost::boost_unit_test_framework 存在,但是 ld 链接时,发生 multiple definition 错误
  2. 判断 lib 是否存在

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
     # Global approach
     if(ZLIB_FOUND)
        include_directories(${ZLIB_INCLUDE_DIRS})
        target_link_libraries (helloworld ${ZLIB_LIBRARIES})
     endif()
    
     # Modern CMake targets approach
     if(TARGET ZLIB::ZLIB)
        target_link_libraries(helloworld ZLIB::ZLIB)
     endif()

FAQ

缺失依赖处理

参考:

问题描述:

  • conan 找不到 prebuilt package(包)

快速解决方法:

  • conan install .. --build missing

    • 本地编译缺失的包

官方推荐方法:

  • 创建自己的 recipe 自己编译

RPATH 在 cmake –install 时报错

例子:

1
2
3
cmake . -Bbuild --install-prefix /tmp/install-destination

cmake --install ./build

报错:

file RPATH_CHANGE could not write new RPATH:
...
...

解决方法: 参考:

1
2
3
4
5
6
7
if (APPLE)
    set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
else()
    set(CMAKE_INSTALL_RPATH "$ORIGIN/../lib")
endif()

set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)

cmake uninstall target 方法

参考:

说明:

  1. cmake 不提供 uninstall target
  2. 快速解决方法:

    1
    
    xargs rm < install_manifest.txt