[conan] cmake_find_package生成器

原文链接:https://www.yuque.com/softdev/cpptools/wyoliy

参考文章:

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

引言

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

 [requires]
 poco/1.9.4

 [generators]
 cmake	#使用cmake生成器

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

cmake_minimum_required(VERSION 2.8.12)
project(MD5Encrypter)

add_definitions("-std=c++11")

#添加conan
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup() #conan初始设置

add_executable(md5 md5.cpp)
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即可

from conans import ConanFile, CMake, tools

class LibConan(ConanFile):
    ...
    requires = "zlib/1.2.11"
    generators = "cmake_find_package"	#指定生成器
    
    def build(self):
        cmake = CMake(self) # it will find the packages by using our auto-generated FindXXX.cmake files
        cmake.configure()
        cmake.build()

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

cmake_minimum_required(VERSION 3.0)
project(helloworld)
add_executable(helloworld hello.c)
find_package(ZLIB)

# 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()

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. CMakeList.txt中添加一句话
cmake_minimum_required(VERSION 3.0)
project(helloworld)

include(${CMAKE_BINARY_DIR}/conan_paths.cmake)

add_executable(helloworld hello.c)
find_package(ZLIB)

# 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()

方法二
  1. conanfile.txt中修改生成器
 [requires]
 zlib/1.2.11
 ...

 [generators]
 cmake_find_package
  1. 添加find_package搜索路径
 cmake_minimum_required(VERSION 3.0)
 project(helloworld)
 
 list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
 list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})

 add_executable(helloworld hello.c)
 find_package(ZLIB)

 # 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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

geodoer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值