Conan 的配置与使用
ssk-wh Lv5

本文面向已经“听说过 Conan”的同学,帮你把“能跑起来”到“能上线”的每一步都踩实。官方文档很香,但太长;这篇博客很干,但够短。


官网指路
Conan 官网:https://conan.io/
可用包速查:https://conan.io/center
官方完整文档(建议完整阅读一遍):https://docs.conan.io/2/index.html


1. 30 秒完成环境初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1. 装 Conan(Python3 自备)
pip3 install "conan>=2.17.0"

# 2. 生成默认 profile(只跑一次)
conan profile detect --force

# 3. 拉依赖、编缺失
conan install . --build=missing -s build_type=Release

# 4. 本地打包(可跳过)
conan create . --user=myname

# 5. 推到私有仓库
conan upload ffmpeg/5.0 -r myrepo

2. 先把项目摆成“Conan 喜欢的样子”

以 CMake 工程为例,推荐目录:

1
2
3
4
conan-fmt-demo/
├── conanfile.txt # 或 conanfile.py,二选一
├── CMakeLists.txt
└── main.cpp

最小可用 conanfile.txt

1
2
3
4
5
6
[requires]
fmt/10.1.1

[generators]
CMakeDeps
CMakeToolchain

说明

  • fmt/10.1.1:告诉 Conan“我要这个版本”。
  • CMakeDeps + CMakeToolchain:自动生成 fmt-config.cmake 和 toolchain 文件,CMake 一句 find_package(fmt) 就能用。

想知道还有哪些版本?两条路:

1
2
conan search fmt                  # 本地+远端一起搜
# 或者打开浏览器:https://conan.io/center

3. 一条命令安装全部依赖

1
conan install . --build=missing -s build_type=Release
  • --build=missing:远端没有现成二进制就本地现编。
  • 成功后会在 build/Release/generators/ 里吐出 conan_toolchain.cmake

4. 编译项目:两种姿势,任选

姿势 A:经典手写

1
2
3
4
5
cmake -G "Unix Makefiles" \
-DCMAKE_TOOLCHAIN_FILE=build/Release/generators/conan_toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-S . -B build/Release
cmake --build build/Release -j$(nproc)

姿势 B:CMake ≥3.19 的 preset(真香)

1
2
cmake --preset conan-default
cmake --build --preset conan-release

老版本 CMake 想用 preset?官方教你曲线救国:https://docs.conan.io/2/tutorial/consuming_packages/use_tools_as_conan_packages.html


5. 踩坑现场:缺失系统库怎么办?

典型报错:

1
ERROR: vaapi/system … No package 'libva' found

原因:vaapi/system 这类 system不会帮你编 libva,它假设系统里已经装好。
解决(Ubuntu/Debian 演示):

1
2
3
4
5
6
7
8
9
10
# 1. 装 apt-file 并更新索引
sudo apt update && sudo apt install apt-file
sudo apt-file update

# 2. 找到提供 libva.pc 的包
apt-file search libva.pc
# 输出:libva-dev: /usr/lib/.../libva.pc

# 3. 装它
sudo apt install libva-dev

如果缺失的不止一个,可以偷懒用脚本:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env bash
# 暴力补齐任意 system 包缺失的 .pc 系统依赖
# Ubuntu/Debian 专用

set -euo pipefail

# ======== 用户可编辑区域 ========
SYSTEM_PKGS=(
xorg/system
vaapi/system
vdpau/system

# 继续往这里加其他 system 包
)
# ================================

echo "=== 开始暴力补齐 system 包缺失依赖 ==="

# 确保 apt-file 存在且索引最新
if ! command -v apt-file &>/dev/null; then
sudo apt-get update && sudo apt-get install -y apt-file
sudo apt-file update
fi

declare -A SEEN # 记录已处理过的 .pc,避免死循环

pip3 install conan>=2.17.0

echo "检测本地编译工具"
conan profile detect --force

for sys_pkg in "${SYSTEM_PKGS[@]}"; do
echo "🔍 检查 $sys_pkg ..."
while :; do
OUTPUT=$(mktemp)
trap 'rm -f "$OUTPUT"' EXIT

set +e
conan install --requires="$sys_pkg" \
-c tools.system.package_manager:mode=check \
-c tools.system.package_manager:sudo=False \
>"$OUTPUT" 2>&1
RET=$?
set -e

# 提取所有缺失的 .pc 名
mapfile -t MISSING < <(grep -oP "No package '\K[^']+" "$OUTPUT" | sort -u)

[[ ${#MISSING[@]} -eq 0 ]] && { echo "✅ $sys_pkg 已满足"; break; }

for pc in "${MISSING[@]}"; do
[[ -n "${SEEN[$pc]:-}" ]] && continue
SEEN[$pc]=1

echo "🔍 查找缺失 .pc:$pc.pc"
PKG=$(apt-file search "$pc.pc" | awk -F: '{print $1}' | sort -u)

case $(echo "$PKG" | wc -l) in
0)
echo "⚠️ 未找到包含 $pc.pc 的包,跳过"
continue
;;
1)
echo "📦 安装 $PKG"
sudo apt-get install -y "$PKG"
;;
*)
echo "❓ 多个候选包:$PKG"
echo " 请手动处理:sudo apt install <包名>"
;;
esac
done
done
done

echo "=== 所有 system 包依赖已补齐 ==="

脚本会自动扫描 xorg/systemvaapi/systemvdpau/system 等常见 system 包,缺啥补啥,直到全部绿灯。


6. 私有仓库:又快又稳

6.1 添加仓库

1
conan remote add myrepo https://your-private-repo.com --index=0

--index=0 把私有仓库置顶,优先级最高。
登录(仅上传时需要):

1
conan remote login myrepo <user> -p <pass>

6.2 查看 / 调整顺序

1
2
3
conan remote list
conan remote remove myrepo # 先删
conan remote add myrepo <url> --index=0

7. 把包推上去

场景 A:官方包直接迁到私有

1
2
conan install --requires=ffmpeg/5.0@ --build=missing
conan upload ffmpeg/5.0 -r myrepo --confirm

场景 B:自研项目

1
2
conan create . --user=mychannel
conan upload mylib/1.0@mychannel -r myrepo --confirm

8. 命令小抄(建议收藏)

目的 命令
查看本地缓存 conan list '*'
删除本地包 conan remove <ref>
搜索远端包 conan search <pkg> -r myrepo
查看 profile 路径 conan profile path default
编辑 profile conan profile edit default

9. 结语

  • 装好 Conan → profile detectconan install,项目就跑起来了。
  • 缺系统库 → apt-file-dev 包,或者一键脚本。
  • 私有仓库 → remote add --index=0 置顶,上传一条命令搞定。

祝你编译愉快,一路无坑!

 Comments
Comment plugin failed to load
Loading comment plugin