conan-vcpkg
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseConan and vcpkg
Conan 和 vcpkg
Purpose
用途
Guide agents through C/C++ dependency management with Conan and vcpkg: declaring dependencies, integrating with CMake, managing binary compatibility, and choosing the right tool for a given project.
指导开发者使用Conan和vcpkg进行C/C++依赖管理:声明依赖、与CMake集成、管理二进制兼容性,以及为特定项目选择合适的工具。
Triggers
触发场景
- "How do I add a C++ library dependency with Conan?"
- "How do I use vcpkg with CMake?"
- "What's the difference between Conan and vcpkg?"
- "How do I add zlib/openssl/fmt with a package manager?"
- "How do I create a vcpkg.json manifest?"
- "My Conan package build is failing — how do I debug it?"
- “如何使用Conan添加C++库依赖?”
- “如何将vcpkg与CMake配合使用?”
- “Conan和vcpkg有什么区别?”
- “如何使用包管理器添加zlib/openssl/fmt?”
- “如何创建vcpkg.json清单文件?”
- “我的Conan包构建失败了——该如何调试?”
Workflow
操作流程
1. Conan vs vcpkg decision
1. Conan与vcpkg的选择
text
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)text
选择哪个包管理器?
├── 团队主要在Windows上使用MSVC → vcpkg(对MSVC的集成更好)
├── 需要二进制包(CI中无需源码构建) → Conan(支持二进制缓存)
├── 需要交叉编译支持 → Conan(配置文件)或基于Zig的构建
├── 需要特定版本的包 → Conan(灵活的版本控制)
├── 快速搭建项目,只需要能运行即可 → vcpkg(更简单)
└── 开源项目,受众广泛 → vcpkg(与GitHub集成)2. vcpkg setup
2. vcpkg安装配置
bash
undefinedbash
undefinedClone vcpkg
Clone vcpkg
git clone https://github.com/microsoft/vcpkg.git
./vcpkg/bootstrap-vcpkg.sh # Linux/macOS
./vcpkg/bootstrap-vcpkg.bat # Windows
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
./vcpkg/vcpkg install zlib curl openssl
Integrate with CMake
与CMake集成
cmake -S . -B build
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
undefinedcmake -S . -B build
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
undefined3. vcpkg manifest mode (recommended)
3. vcpkg清单模式(推荐)
json
// 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..."
}cmake
undefinedjson
// vcpkg.json — 放置在项目根目录
{
"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..."
}cmake
undefinedCMakeLists.txt
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)
```bashcmake_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)
```bashBuild — vcpkg automatically installs dependencies
构建 — vcpkg会自动安装依赖
cmake -S . -B build
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake cmake --build build
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake cmake --build build
undefinedcmake -S . -B build
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake cmake --build build
-DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake cmake --build build
undefined4. Conan setup
4. Conan安装配置
bash
undefinedbash
undefinedInstall Conan
安装Conan
pip install conan
pip install conan
Set up default profile (detects compiler, OS)
设置默认配置文件(自动检测编译器、操作系统)
conan profile detect
conan profile detect
Check your profile
查看配置文件
conan profile show
undefinedconan profile show
undefined5. Conan with CMake (Conan 2.x)
5. Conan与CMake配合使用(Conan 2.x)
ini
undefinedini
undefinedconanfile.txt
conanfile.txt
[requires]
zlib/1.3
fmt/10.2.1
openssl/3.2.0
[generators]
CMakeDeps
CMakeToolchain
[options]
openssl/*:shared=False
```bash[requires]
zlib/1.3
fmt/10.2.1
openssl/3.2.0
[generators]
CMakeDeps
CMakeToolchain
[options]
openssl/*:shared=False
```bashInstall dependencies
安装依赖
conan install . --output-folder=build --build=missing
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
-DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake
-DCMAKE_BUILD_TYPE=Release cmake --build build
```cmakecmake -S . -B build
-DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake
-DCMAKE_BUILD_TYPE=Release cmake --build build
-DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake
-DCMAKE_BUILD_TYPE=Release cmake --build build
```cmakeCMakeLists.txt
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
)
undefinedfind_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
)
undefined6. Conan profiles for cross-compilation
6. 用于交叉编译的Conan配置文件
ini
undefinedini
undefined~/.conan2/profiles/linux-arm64
~/.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]
[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)
Tools that run on build machine (x86)
```bash
```bashCross-compile
交叉编译
conan install .
--profile:build=default
--profile:host=linux-arm64
--output-folder=build-arm
--build=missing
--profile:build=default
--profile:host=linux-arm64
--output-folder=build-arm
--build=missing
undefinedconan install .
--profile:build=default
--profile:host=linux-arm64
--output-folder=build-arm
--build=missing
--profile:build=default
--profile:host=linux-arm64
--output-folder=build-arm
--build=missing
undefined7. conanfile.py (advanced)
7. conanfile.py(进阶用法)
python
undefinedpython
undefinedconanfile.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()undefinedfrom 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()undefined8. Common dependency lookup
8. 常见依赖对照表
| Library | vcpkg name | Conan name |
|---|---|---|
| zlib | | |
| OpenSSL | | |
| libcurl | | |
| {fmt} | | |
| spdlog | | |
| Boost | | |
| nlohmann-json | | |
| googletest | | |
| Google Benchmark | | |
| SQLite | | |
| protobuf | | |
For vcpkg baseline pinning and Conan binary cache setup, see references/package-manager-patterns.md.
| 库 | vcpkg名称 | Conan名称 |
|---|---|---|
| zlib | | |
| OpenSSL | | |
| libcurl | | |
| {fmt} | | |
| spdlog | | |
| Boost | | |
| nlohmann-json | | |
| googletest | | |
| Google Benchmark | | |
| SQLite | | |
| protobuf | | |
关于vcpkg基线固定和Conan二进制缓存配置,请参考references/package-manager-patterns.md。
Related skills
相关技能
- Use for CMake integration with both Conan and vcpkg
skills/build-systems/cmake - Use for cross-compilation with Conan profiles
skills/compilers/cross-gcc - Use as the backend for package-managed projects
skills/build-systems/ninja
- 若需Conan和vcpkg与CMake的集成,请使用
skills/build-systems/cmake - 若需结合Conan配置文件进行交叉编译,请使用
skills/compilers/cross-gcc - 若需将包管理项目的构建后端设置为Ninja,请使用
skills/build-systems/ninja