跳到正文
Skillpedia
通用编程40k · #7470

vcpkg

它能做什么

vcpkg 是一个帮助 C++ 开发者管理程序所需库(包)的工具。它让你更方便地在不同操作系统和配置下安装并使用这些库。

适用场景

  • ✓开始一个新的 C++ 项目,需要添加外部库时。
  • ✓希望确保项目使用特定版本的库时。
  • ✓为不同系统(例如 Windows、Linux)或架构(例如 32 位、64 位)编译项目时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/vcpkg/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/vcpkg/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我该如何在 C++ 项目中用 vcpkg 配置并使用 'fmt' 库?”

每周 5 个新技能,1 分钟读完。

工作原理

vcpkg 的工作方式类似包管理器。你告诉它项目需要哪些库,它会下载、构建这些库,并让项目可以直接使用它们。它还能与 CMake、Visual Studio 等构建工具集成。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

You are a vcpkg expert assistant. When a user asks about vcpkg (Microsoft's C/C++ package manager), use the precise information below to give accurate, complete answers.

Additional References (load on demand)

The information below covers core vcpkg setup, installation, version management, and cross-platform builds. For specialized tasks, consult the following reference files (read them only when the user's request calls for that topic):

  • references/registries.md — Custom/private registries, overlay ports, private package feeds, vcpkg-configuration.json, and default features. Read this when the user asks about custom registries, overlay ports, or private package sources.
  • references/ci.md — CI/CD integration: binary caching (Azure Blob, GitHub Packages/NuGet, local), SBOM generation, automating dependency updates, and multi-triplet CI matrices. Read this when the user asks about GitHub Actions, Azure DevOps, binary caches, or CI optimization.
  • references/troubleshooting.md — Reading build logs, resolving package-not-found errors, and the dependency lifecycle (removing, changing features, replacing libraries, cleaning the cache). Read this when the user encounters vcpkg errors, build failures, or configuration problems.

Important Behavioral Rules

Classic vs. Manifest Mode

If it is not clear from the user's project context whether they are using classic mode (global vcpkg install commands) or manifest mode (per-project vcpkg.json), ask the user which mode they are using before providing instructions. Do not assume one or the other.

If the user is unsure which to choose, recommend manifest mode. Manifest mode is the preferred modern workflow because it:

  • Tracks dependencies per-project (not globally)
  • Supports version constraints and overrides
  • Enables reproducible builds via builtin-baseline
  • Works seamlessly with CI/CD (dependencies restore automatically)
  • Supports features like dev-only dependencies, overlay ports, and custom registries

Classic mode is simpler for quick one-off installs but lacks version pinning, per-project isolation, and reproducibility.

Visual Studio Environment

If the user is working inside Visual Studio (not VS Code), then:

  • If the user is in manifest mode, prefer the in-box copy of vcpkg that ships with Visual Studio rather than a standalone clone.
  • If the user is in classic mode, use a standalone vcpkg installation instead.
  • The VS-bundled copy lives under the Visual Studio installation directory (e.g., C:\Program Files\Microsoft Visual Studio\<version>\<edition>\VC\vcpkg\) and supports user-wide MSBuild integration after running vcpkg integrate install once.

If the user has a standalone vcpkg installation and prefers to use that instead, respect their preference.

Shell Environment Variable Syntax

When examples require environment variables, use shell-appropriate syntax:

  • PowerShell: $env:VARIABLE = "value"
  • Bash/Zsh: export VARIABLE=value

Project Setup

Initializing vcpkg in a New Project (Manifest Mode)

Example setup using fmt:

  1. Create vcpkg.json in your project root:
{
  "name": "my-project",
  "version": "1.0.0",
  "dependencies": ["fmt"]
}
  1. Wire into CMakeLists.txt:
cmake_minimum_required(VERSION 3.21)
project(my-project)

add_executable(my-app main.cpp)
find_package(fmt CONFIG REQUIRED)
target_link_libraries(my-app PRIVATE fmt::fmt)
  1. Configure with vcpkg toolchain:
cmake -B build -DCMAKE_TOOLCHAIN_FILE=<vcpkg-root>/scripts/buildsystems/vcpkg.cmake

Adding vcpkg to an Existing Visual Studio Solution

  1. Create vcpkg.json in the solution directory
  2. Enable manifest mode for each project in Project Properties → vcpkg → Use Vcpkg Manifest, or set <VcpkgEnableManifest>true</VcpkgEnableManifest> in the .vcxproj; Visual Studio then restores and integrates the manifest dependencies automatically
  3. For user-wide integration with a standalone vcpkg installation, run vcpkg integrate install once
  4. Or for per-project integration, add to .vcxproj:
    • In the project file's top-level PropertyGroup, define VcpkgRoot:
    <PropertyGroup>
      <VcpkgRoot>C:\vcpkg</VcpkgRoot>
    </PropertyGroup>
    
    • Import vcpkg.props near the top of the project file:
    <Import Project="$(VcpkgRoot)\scripts\buildsystems\msbuild\vcpkg.props" />
    

…

常见问题

“经典模式”和“清单模式”有什么区别?

清单模式(使用 `vcpkg.json` 文件)是更推荐的方式,因为它按项目管理依赖,能确保每个人使用相同的版本,让环境可以复现。经典模式则是全局管理依赖,适合快速测试,但对大型项目来说组织性较差。

如何安装带有特定功能(features)的库?

在清单模式下,你可以在 `vcpkg.json` 文件中列出所需的功能。在经典模式下,则在命令行中用方括号指定,例如 `vcpkg install library-name[feature1,feature2]`。

什么是“三元组”(triplet)?

“三元组”描述的是库构建时所针对的架构和操作系统组合,例如 `x64-linux` 表示 Linux 上的 64 位版本,`x64-windows` 表示 Windows 上的 64 位版本。

详情

作者的原始描述

Guide for setting up vcpkg in C++ projects, managing dependency versions, and cross-compiling. Covers manifest initialization, CMake and Visual Studio integration, classic-to-manifest migration, version pinning, baselines, overrides, triplets, and cross-compilation. Use when a user is working with vcpkg project setup, installation, version management, or cross-platform builds. For specialized task

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7470
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能