参考文章:

  1. integrations/build_system/cmake/cmake_find_package_generator
  2. reference/generators/cmake_find_package

引言

如果使用cmake生成器(可查看HiConan文章)

  1. [requires]
  2. poco/1.9.4
  3. [generators]
  4. cmake #使用cmake生成器

在cmake中,需要这样来修改代码

  1. cmake_minimum_required(VERSION 2.8.12)
  2. project(MD5Encrypter)
  3. add_definitions("-std=c++11")
  4. #添加conan
  5. include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
  6. conan_basic_setup() #conan初始设置
  7. add_executable(md5 md5.cpp)
  8. target_link_libraries(md5 ${CONAN_LIBS}) #链接你使用conan安装的所有lib文件

这样做有以下缺点:

  1. 与原始的cmakelists.txt写法不同,因此我们无法从原始的cmake文件中无缝切换成conan,也无法无缝地切换回去
  2. 所有的target都会链接到conan安装的所有库

cmake_find_package生成器

conan提供cmake_find_package生成器,它使得我们能够使用find_package的方式来查找包,从而保持和原来一样。

conanfile.py

如果你在项目中使用conanfile.py管理依赖包,可以参照这个来使用cmake_find_package

只需一步,在conanfile.py中将生成器改为cmake_find_package即可

  1. from conans import ConanFile, CMake, tools
  2. class LibConan(ConanFile):
  3. ...
  4. requires = "zlib/1.2.11"
  5. generators = "cmake_find_package" #指定生成器
  6. def build(self):
  7. cmake = CMake(self) # it will find the packages by using our auto-generated FindXXX.cmake files
  8. cmake.configure()
  9. cmake.build()

在cmake中,像当初使用find_package一样编写即可

  1. cmake_minimum_required(VERSION 3.0)
  2. project(helloworld)
  3. add_executable(helloworld hello.c)
  4. find_package(ZLIB)
  5. # Global approach
  6. if(ZLIB_FOUND)
  7. include_directories(${ZLIB_INCLUDE_DIRS})
  8. target_link_libraries (helloworld ${ZLIB_LIBRARIES})
  9. endif()
  10. # Modern CMake targets approach
  11. if(TARGET ZLIB::ZLIB)
  12. target_link_libraries(helloworld ZLIB::ZLIB)
  13. endif()

2022/3/25注:虽然官网这么说,但很像cmake还是找不到,不知道为什么。我通过本文conanfile.txt>方法二解决了此问题

conanfile.txt

如果你在项目中使用conanfile.txt来管理依赖库,需要做的事情就比较多了。

方法一

  1. 修改conanfile.txt中的生成器 ``` [requires] zlib/1.2.11 …

[generators] cmake_find_package cmake_paths

  1. 2. `CMakeList.txt`中添加一句话
  2. ```cmake
  3. cmake_minimum_required(VERSION 3.0)
  4. project(helloworld)
  5. include(${CMAKE_BINARY_DIR}/conan_paths.cmake)
  6. add_executable(helloworld hello.c)
  7. find_package(ZLIB)
  8. # Global approach
  9. if(ZLIB_FOUND)
  10. include_directories(${ZLIB_INCLUDE_DIRS})
  11. target_link_libraries (helloworld ${ZLIB_LIBRARIES})
  12. endif()
  13. # Modern CMake targets approach
  14. if(TARGET ZLIB::ZLIB)
  15. target_link_libraries(helloworld ZLIB::ZLIB)
  16. endif()

方法二

  1. conanfile.txt中修改生成器

    1. [requires]
    2. zlib/1.2.11
    3. ...
    4. [generators]
    5. cmake_find_package
  2. 添加find_package搜索路径

    1. cmake_minimum_required(VERSION 3.0)
    2. project(helloworld)
    3. list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
    4. list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
    5. add_executable(helloworld hello.c)
    6. find_package(ZLIB)
    7. # Global approach
    8. if(ZLIB_FOUND)
    9. include_directories(${ZLIB_INCLUDE_DIRS})
    10. target_link_libraries (helloworld ${ZLIB_LIBRARIES})
    11. endif()
    12. # Modern CMake targets approach
    13. if(TARGET ZLIB::ZLIB)
    14. target_link_libraries(helloworld ZLIB::ZLIB)
    15. endif()