关于 conan-vcpkg
面向 Agent 的 C/C++ 依赖管理技能,指导使用 Conan 和 vcpkg 声明依赖、接入 CMake、处理二进制兼容性并选择包管理器。
主要功能
- 01比较 Conan 与 vcpkg 的适用场景
- 02配置 vcpkg 清单和依赖
- 03使用 Conan 配置及安装依赖
- 04通过 Conan profiles 交叉编译
- 05排查包构建与依赖问题
适用场景
- 01为 CMake 项目添加 C/C++ 库
- 02根据团队需求选择包管理器
- 03配置 Conan 交叉编译目标
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mohitmishra786/ low-level-dev-skills --skill conan-vcpkg安装后在 Claude Code 中输入/conan-vcpkg确认已加载。
附带文件references/package-manager-patterns.md
详细介绍
Conan 和 vcpkg
用途
指导 Agent 使用 Conan 和 vcpkg 管理 C/C++ 依赖:声明依赖、与 CMake 集成、管理二进制兼容性,以及为特定项目选择合适的工具。
触发条件
- “如何使用 Conan 添加 C++ 库依赖?”
- “如何在 CMake 中使用 vcpkg?”
- “Conan 和 vcpkg 有什么区别?”
- “如何使用包管理器添加 zlib/openssl/fmt?”
- “如何创建 vcpkg.json 清单?”
- “我的 Conan 包构建失败了——如何调试?”
工作流
1. 选择 Conan 还是 vcpkg
Which package manager?
├── Team uses MSVC on Windows primarily → vcpkg (better MSVC integration)
├── Need binary packages (no source builds in CI) → Conan (binary cache)
├── Need cross-compilation support → Conan (profiles) or Zig-based builds
├── Need a specific version of a package → Conan (flexible versioning)
├── Quick project setup, just need it to work → vcpkg (simpler)
└── Open-source project, broad audience → vcpkg (GitHub-integrated)
2. 配置 vcpkg
# Clone vcpkg
git clone https://github.com/microsoft/vcpkg.git
./vcpkg/bootstrap-vcpkg.sh # Linux/macOS
./vcpkg/bootstrap-vcpkg.bat # Windows
# Install packages (classic mode)
./vcpkg/vcpkg install zlib curl openssl
# Integrate with CMake
cmake -S . -B build \
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
3. vcpkg 清单模式(推荐)
// vcpkg.json — place at project root
{
"name": "myapp",
"version": "1.0.0",
"dependencies": [
"zlib",
"curl",
{ "name": "openssl", "version>=": "3.0.0" },
{ "name": "boost-filesystem", "platform": "!windows" },
{
"name": "fmt",
"features": ["core"]
}
],
"builtin-baseline": "abc123..."
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(myapp)
find_package(ZLIB REQUIRED)
find_package(CURL REQUIRED)
find_package(fmt REQUIRED)
add_executable(myapp src/main.cpp)
target_link_libraries(myapp PRIVATE ZLIB::ZLIB CURL::libcurl fmt::fmt)
# Build — vcpkg automatically installs dependencies
cmake -S . -B build \
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build build
4. 配置 Conan
# Install Conan
pip install conan
# Set up default profile (detects compiler, OS)
conan profile detect
# Check your profile
conan profile show
5. 将 Conan 与 CMake 配合使用(Conan 2.x)
# conanfile.txt
[requires]
zlib/1.3
fmt/10.2.1
openssl/3.2.0
[generators]
CMakeDeps
CMakeToolchain
[options]
openssl/*:shared=False
# Install dependencies
conan install . --output-folder=build --build=missing
# Configure and build
cmake -S . -B build \
-DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release
cmake --build build
# CMakeLists.txt
find_package(ZLIB REQUIRED)
find_package(fmt REQUIRED)
find_package(OpenSSL REQUIRED)
add_executable(myapp src/main.cpp)
target_link_libraries(myapp PRIVATE
ZLIB::ZLIB
fmt::fmt
OpenSSL::SSL OpenSSL::Crypto
)
6. 用于交叉编译的 Conan 配置文件
# ~/.conan2/profiles/linux-arm64
[settings]
os=Linux
arch=armv8
compiler=gcc
compiler.version=12
compiler.libcxx=libstdc++11
build_type=Release
[buildenv]
CC=aarch64-linux-gnu-gcc
CXX=aarch64-linux-gnu-g++
[tool_requires]
# Tools that run on build machine (x86)
# Cross-compile
conan install . \
--profile:build=default \
--profile:host=linux-arm64 \
--output-folder=build-arm \
--build=missing
7. conanfile.py(高级)
# conanfile.py
from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMakeDeps, CMake
class MyAppConan(ConanFile):
name = "myapp"
version = "1.0"
settings = "os", "compiler", "build_type", "arch"
def requirements(self):
self.requires("zlib/1.3")
self.requires("fmt/10.2.1")
if self.settings.os == "Linux":
self.requires("openssl/3.2.0")
def generate(self):
tc = CMakeToolchain(self)
tc.generate()
deps = CMakeDeps(self)
deps.generate()
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
8. 常见依赖查询
| 库 | vcpkg 名称 | Conan 名称 |
|---|---|---|
| zlib | zlib | zlib/1.3 |
| OpenSSL | openssl | openssl/3.2.0 |
| libcurl | curl | libcurl/8.4.0 |
| {fmt} | fmt | fmt/10.2.1 |
| spdlog | spdlog | spdlog/1.12.0 |
| Boost | boost | boost/1.83.0 |
| nlohmann-json | nlohmann-json | nlohmann_json/3.11.3 |
| googletest | gtest | gtest/1.14.0 |
| Google Benchmark | benchmark | benchmark/1.8.3 |
| SQLite | sqlite3 | sqlite3/3.44.0 |
| protobuf | protobuf | protobuf/4.25.1 |
有关 vcpkg 基线固定和 Conan 二进制缓存配置,请参阅 references/package-manager-patterns.md。
相关技能
- 使用
skills/build-systems/cmake了解 Conan 和 vcpkg 的 CMake 集成 - 使用
skills/compilers/cross-gcc了解如何使用 Conan 配置文件进行交叉编译 - 使用
skills/build-systems/ninja作为包管理项目的后端
评论
支持 Markdown