(目录)
一、下载及安装
首先安装python3,然后
# 安装
$ pip install conan
# 升级
$ pip install conan --upgrade
二、使用
1. CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(compressor C)
find_package(ZLIB REQUIRED)
add_executable(${PROJECT_NAME} src/main.c)
target_link_libraries(${PROJECT_NAME} ZLIB::ZLIB)
如何确定库的名字? 首先访问https://conan.io/center,然后搜索库。
找到描述库依赖的python代码,然后找到
def package_info(self):
self.cpp_info.set_property("cmake_file_name", "ZLIB")
self.cpp_info.set_property("cmake_target_name", "ZLIB::ZLIB")
find_package()中的库名就是上边的cmake_file_name,而target_link_libraries()中的依赖就是上边的cmake_target_name。
2. conanfile.txt
[requires]
# 需要安装的依赖库
zlib/1.2.11
[generators]
CMakeDeps
CMakeToolchain
3. 第一次使用时生成conan profile
$ conan profile detect --force
生成的profile文件为~/.conan2/profiles/default
4. 安装依赖库
$ conan install . --output-folder=build --build=missing
5. 编译
$ cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
$ cmake --build build
三、参考URL
- 库查找中心 CONAN Center:https://conan.io/center
- CONAN2手册:https://docs.conan.io/2/tutorial/consuming_packages/build_simple_cmake_project.html