本文属于 C++ 系列文章,本篇文章,是在 Quickstart C++ with cmake, visualstudio | CPP 基础上,继续的。
目录
- vcpkg
- 总结
- 安装
- 安装 mingw64
- 安装 vcpkg
- 创建项目
- 查询已有的包
- 在 Visual Studio 中调试
- 发布依赖
- Trouble Shooting
- CMake Error: CMake was unable to find a build program corresponding to "Ninja"
- No CMAKE_CXX_COMPILER could be found
- Links
vcpkg
vcpkg 是 C++ 项目的包管理工具,类似于 pip 之于 Python, npm 之于 Node.js, maven 之于 Java.
总结
- Windows 11 上使用了 MSVC 兼容性没有 mingw64 好,配置 MSVC 花了好长时间没弄好,转而使用 mingw64 很快就配置好了
- Visual Studio 使用体验很好
- 以下脚本是在
C:\devel\msys64\mingw64.exe
控制台下执行 - GitHub, 本文中的代码放在了 Repo https://github.com/hailiang-wang/vcpkg-visualstudio-quickstart
安装
安装 mingw64
Using GCC with MinGW
https://code.visualstudio.com/docs/cpp/config-mingw
安装 vcpkg
https://learn.microsoft.com/en-us/vcpkg/users/platforms/mingw
https://learn.microsoft.com/en-us/vcpkg/get_started/get-started-vs?pivots=shell-powershell
# 首先,进入 mingw64
export VCPKG_DEFAULT_TRIPLET=x64-mingw-static
export VCPKG_DEFAULT_HOST_TRIPLET=x64-mingw-static
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
export PATH=/c/Windows/System32/WindowsPowerShell/v1.0:$PATH
sh ./bootstrap-vcpkg.sh
设置环境变量
$env:VCPKG_ROOT="C:\path\to\vcpkg"
$env:PATH="$env:VCPKG_ROOT;$env:PATH"
从终端运行:
创建项目
首先,创建一个文件夹:
mkdir app
第二步,初始化这个项目的 vcpkg 配置:
cd app
vcpkg new --application
The vcpkg new command adds a vcpkg.json file and a vcpkg-configuration.json file in the project’s directory.
第三步,添加依赖包:
vcpkg add port fmt
第四步,使用 cmake
: 创建 CMakePresets.json
, CMakeUserPresets.json
, CMakeLists.txt
这两个文件属于 CMake 的配置文件,参考:https://cmake.org/cmake/help/latest/manual/cmake-presets.7.html
- CMakePresets.json
{
"version": 3,
"configurePresets": [
{
"name": "vcpkg",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
}
}
]
}
- CMakeUserPresets.json
{
"version": 3,
"configurePresets": [
{
"name": "default",
"inherits": "vcpkg",
"environment": {
"VCPKG_ROOT": "C:\\devel\\Microsoft\\vcpkg",
"C": "C:\\devel\\msys64\\mingw64\\bin\\gcc.exe",
"CXX": "C:\\devel\\msys64\\mingw64\\bin\\g++.exe",
"CMT_MINGW_PATH": "C:\\devel\\msys64\\mingw64\\bin",
"VCPKG_TARGET_TRIPLET": "x64-mingw-static",
"VCPKG_DEFAULT_HOST_TRIPLET": "x64-mingw-static"
}
}
]
}
- CMakeLists.txt
注意:此处指定了 VCPKG_TARGET_TRIPLET,如果这里不声明,则会造成 build 失败,参考 LINK。
cmake_minimum_required (VERSION 3.8)
set(VCPKG_TARGET_TRIPLET x64-mingw-static)
project(HelloWorld)
find_package(fmt CONFIG REQUIRED)
add_executable(HelloWorld helloworld.cpp)
target_link_libraries(HelloWorld PRIVATE fmt::fmt)
第五步,执行构建 build
添加 helloworld.cpp 脚本
#include <fmt/core.h>
int main()
{
fmt::print("Hello World!\n");
return 0;
}
生成构建文件及完成构建
cmake --preset=default # 生成 build dir
cmake --build build # 进行构建
在此期间,可能会下载 PowerShell, 因为默认的 PowerShell 版本太低。
┌───( AdministratorAdministrator @ ZIHUATANEJO MinGw )-[ /home/Administrator/git/visualstudioexamples/vcpkg-visualstudio-quickstart ] Mon Jan 20 15:20:42 master
└─ $ cmake --preset=default
Preset CMake variables:
CMAKE_TOOLCHAIN_FILE="C:\devel\Microsoft\vcpkg/scripts/buildsystems/vcpkg.cmake"
Preset environment variables:
C="C:\devel\msys64\mingw64\bin\gcc.exe"
CMT_MINGW_PATH="C:\devel\msys64\mingw64\bin"
CXX="C:\devel\msys64\mingw64\bin\g++.exe"
VCPKG_DEFAULT_HOST_TRIPLET="x64-mingw-static"
VCPKG_ROOT="C:\devel\Microsoft\vcpkg"
VCPKG_TARGET_TRIPLET="x64-mingw-static"
-- Running vcpkg install
Fetching registry information from https://github.com/microsoft/vcpkg (HEAD)...
Detecting compiler hash for triplet x64-mingw-static...
Compiler found: C:/devel/msys64/mingw64/bin/x86_64-w64-mingw32-g++.exe
The following packages will be built and installed:
fmt:x64-mingw-static@11.0.2#1 -- C:\Users\Administrator\AppData\Local\vcpkg\registries\git-trees\07a73a7565e5de9eb42e90c16c133bdfdfebbcda
* vcpkg-cmake:x64-mingw-static@2024-04-23 -- C:\Users\Administrator\AppData\Local\vcpkg\registries\git-trees\e74aa1e8f93278a8e71372f1fa08c3df420eb840
* vcpkg-cmake-config:x64-mingw-static@2024-05-23 -- C:\Users\Administrator\AppData\Local\vcpkg\registries\git-trees\97a63e4bc1a17422ffe4eff71da53b4b561a7841
Additional packages (*) will be modified to complete this operation.
Restored 0 package(s) from C:\Users\Administrator\AppData\Local\vcpkg\archives in 231 us. Use --debug to see more details.
Installing 1/3 vcpkg-cmake-config:x64-mingw-static@2024-05-23...
Building vcpkg-cmake-config:x64-mingw-static@2024-05-23...
C:\devel\Microsoft\vcpkg\triplets\community\x64-mingw-static.cmake: info: loaded community triplet from here. Community triplets are not built in the curated registry and are thus less likely to succeed.
C:\Users\Administrator\AppData\Local\vcpkg\registries\git-trees\97a63e4bc1a17422ffe4eff71da53b4b561a7841: info: installing overlay port from here
-- Installing: C:/devel/Microsoft/vcpkg/packages/vcpkg-cmake-config_x64-mingw-static/share/vcpkg-cmake-config/vcpkg_cmake_config_fixup.cmake
-- Installing: C:/devel/Microsoft/vcpkg/packages/vcpkg-cmake-config_x64-mingw-static/share/vcpkg-cmake-config/vcpkg-port-config.cmake
-- Installing: C:/devel/Microsoft/vcpkg/packages/vcpkg-cmake-config_x64-mingw-static/share/vcpkg-cmake-config/copyright
-- Skipping post-build validation due to VCPKG_POLICY_EMPTY_PACKAGE
Stored binaries in 1 destinations in 32.3 ms.
Elapsed time to handle vcpkg-cmake-config:x64-mingw-static: 320 ms
vcpkg-cmake-config:x64-mingw-static package ABI: cf85dd156c7d9949e2fd4502691a332dda8c39d40ee6cb0d9f6b6f356d715d76
Installing 2/3 vcpkg-cmake:x64-mingw-static@2024-04-23...
Building vcpkg-cmake:x64-mingw-static@2024-04-23...
C:\devel\Microsoft\vcpkg\triplets\community\x64-mingw-static.cmake: info: loaded community triplet from here. Community triplets are not built in the curated registry and are thus less likely to succeed.
C:\Users\Administrator\AppData\Local\vcpkg\registries\git-trees\e74aa1e8f93278a8e71372f1fa08c3df420eb840: info: installing overlay port from here
-- Installing: C:/devel/Microsoft/vcpkg/packages/vcpkg-cmake_x64-mingw-static/share/vcpkg-cmake/vcpkg_cmake_configure.cmake
-- Installing: C:/devel/Microsoft/vcpkg/packages/vcpkg-cmake_x64-mingw-static/share/vcpkg-cmake/vcpkg_cmake_build.cmake
-- Installing: C:/devel/Microsoft/vcpkg/packages/vcpkg-cmake_x64-mingw-static/share/vcpkg-cmake/vcpkg_cmake_install.cmake
-- Installing: C:/devel/Microsoft/vcpkg/packages/vcpkg-cmake_x64-mingw-static/share/vcpkg-cmake/vcpkg-port-config.cmake
-- Installing: C:/devel/Microsoft/vcpkg/packages/vcpkg-cmake_x64-mingw-static/share/vcpkg-cmake/copyright
-- Performing post-build validation
Stored binaries in 1 destinations in 26.9 ms.
Elapsed time to handle vcpkg-cmake:x64-mingw-static: 306 ms
vcpkg-cmake:x64-mingw-static package ABI: ee9e51e86e40ce79d99794b64e319b8cde327693db66cd702094716b544af5b7
Installing 3/3 fmt:x64-mingw-static@11.0.2#1...
Building fmt:x64-mingw-static@11.0.2#1...
C:\devel\Microsoft\vcpkg\triplets\community\x64-mingw-static.cmake: info: loaded community triplet from here. Community triplets are not built in the curated registry and are thus less likely to succeed.
C:\Users\Administrator\AppData\Local\vcpkg\registries\git-trees\07a73a7565e5de9eb42e90c16c133bdfdfebbcda: info: installing overlay port from here
-- Using cached fmtlib-fmt-11.0.2.tar.gz.
-- Cleaning sources at C:/devel/Microsoft/vcpkg/buildtrees/fmt/src/11.0.2-c30c0a133f.clean. Use --editable to skip cleaning for the packages you specify.
-- Extracting source C:/devel/Microsoft/vcpkg/downloads/fmtlib-fmt-11.0.2.tar.gz
-- Applying patch fix-write-batch.patch
-- Applying patch fix-pass-utf-8-only-if-the-compiler-is-MSVC-at-build.patch
-- Using source at C:/devel/Microsoft/vcpkg/buildtrees/fmt/src/11.0.2-c30c0a133f.clean
-- Found external ninja('1.12.1').
-- Configuring x64-mingw-static
-- Building x64-mingw-static-dbg
-- Building x64-mingw-static-rel
-- Fixing pkgconfig file: C:/devel/Microsoft/vcpkg/packages/fmt_x64-mingw-static/lib/pkgconfig/fmt.pc
-- Using cached msys2-mingw-w64-x86_64-pkgconf-1~2.3.0-1-any.pkg.tar.zst.
-- Using cached msys2-msys2-runtime-3.5.4-2-x86_64.pkg.tar.zst.
-- Using msys root at C:/devel/Microsoft/vcpkg/downloads/tools/msys2/21caed2f81ec917b
-- Fixing pkgconfig file: C:/devel/Microsoft/vcpkg/packages/fmt_x64-mingw-static/debug/lib/pkgconfig/fmt.pc
-- Installing: C:/devel/Microsoft/vcpkg/packages/fmt_x64-mingw-static/share/fmt/usage
-- Installing: C:/devel/Microsoft/vcpkg/packages/fmt_x64-mingw-static/share/fmt/copyright
-- Performing post-build validation
Stored binaries in 1 destinations in 227 ms.
Elapsed time to handle fmt:x64-mingw-static: 8.3 s
fmt:x64-mingw-static package ABI: 5f12fa0d37c6557e82c8f83ff3f1763610e9ac5cd5cc43de2bb52242975582ad
Total install time: 8.9 s
The package fmt provides CMake targets:
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt)
# Or use the header-only version
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt-header-only)
-- Running vcpkg install - done
-- The C compiler identification is GNU 14.2.0
-- The CXX compiler identification is GNU 14.2.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/devel/msys64/mingw64/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/devel/msys64/mingw64/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (18.2s)
-- Generating done (0.1s)
-- Build files have been written to: C:/Users/Administrator/git/visualstudioexamples/vcpkg-visualstudio-quickstart/build
测试可执行文件。
.\build\HelloWorld.exe
Hello World!
查询已有的包
https://vcpkg.io/en/packages?query=
快速添加依赖。
在 Visual Studio 中调试
使用 Visual Studio 打开 CMakeLists.txt 所在文件夹,会自动识别。
- 支持自动补齐等
发布依赖
自定义一个包发布到 vcpkg.
https://learn.microsoft.com/en-us/vcpkg/produce/publish-to-a-git-registry#1—create-a-vcpkg-registry-using-git
Trouble Shooting
CMake Error: CMake was unable to find a build program corresponding to “Ninja”
执行后,遇到如下错误
└─ $ cmake --preset=default
Preset CMake variables:
CMAKE_TOOLCHAIN_FILE="C:\devel\Microsoft\vcpkg/scripts/buildsystems/vcpkg.cmake"
Preset environment variables:
VCPKG_ROOT="C:\devel\Microsoft\vcpkg"
-- Running vcpkg install
Detecting compiler hash for triplet x64-windows...
Compiler found: C:/devel/MicrosoftVisualStudio/2022/BuildTools/VC/Tools/MSVC/14.42.34433/bin/Hostx64/x64/cl.exe
All requested packages are currently installed.
Total install time: 345 us
The package fmt provides CMake targets:
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt)
# Or use the header-only version
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt-header-only)
-- Running vcpkg install - done
CMake Error: CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
解决方案:配置环境变量
NINJA_HOME=C:\devel\Microsoft\VisualStudio\ide2022community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja
并且添加到 Path 中,类似如下,如果使用 Git Bash, 还需要在 .bashrc 中 export.
No CMAKE_CXX_COMPILER could be found
└─ $ cmake --preset=default
Preset CMake variables:
CMAKE_TOOLCHAIN_FILE="C:\devel\Microsoft\vcpkg/scripts/buildsystems/vcpkg.cmake"
Preset environment variables:
VCPKG_ROOT="C:\devel\Microsoft\vcpkg"
-- Running vcpkg install
Detecting compiler hash for triplet x64-windows...
Compiler found: C:/devel/MicrosoftVisualStudio/2022/BuildTools/VC/Tools/MSVC/14.42.34433/bin/Hostx64/x64/cl.exe
All requested packages are currently installed.
Total install time: 317 us
The package fmt provides CMake targets:
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt)
# Or use the header-only version
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt-header-only)
-- Running vcpkg install - done
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:3 (project):
No CMAKE_C_COMPILER could be found.
Tell CMake where to find the compiler by setting either the environment
variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
the compiler, or to the compiler name if it is in the PATH.
CMake Error at CMakeLists.txt:3 (project):
No CMAKE_CXX_COMPILER could be found.
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
解决方案:mingw64
使用 https://learn.microsoft.com/en-us/vcpkg/users/platforms/mingw
export VCPKG_DEFAULT_TRIPLET=x64-mingw-dynamic
export VCPKG_DEFAULT_HOST_TRIPLET=x64-mingw-dynamic
相关:
https://github.com/microsoft/vcpkg/issues/706
https://stackoverflow.com/questions/63031513/vcpkg-with-mingw
https://learn.microsoft.com/en-us/vcpkg/users/platforms/mingw
Links
- https://gist.github.com/hailiang-wang/abe673e8bdb630403e11a414bc39b2a9