提问人:Michael 提问时间:11/12/2023 最后编辑:Michael 更新时间:11/19/2023 访问量:97
如何使用 vcpkg 安装第三方库?
How to use vcpkg to install third party library?
问:
使用 VCPKG
我跟着踩 https://github.com/David-Haim/concurrencpp#building-installing-and-testing
我首先已经做了vcpkg集成安装
vcpkg.json
{
"name": "vcpkg-test",
"version-string": "1.0.0",
"dependencies": ["concurrencpp"]
}
VS Studio 命令行
vcpkg x-update-baseline --add-initial-baseline
vcpkg install --triplet-x64-windows
D:\project_CPP\C++_study\vcpkg_project\cpp20-test>vcpkg install --triplet x64-windows
Fetching registry information from https://github.com/microsoft/vcpkg (HEAD)...
Detecting compiler hash for triplet x64-windows...
The following packages will be built and installed:
concurrencpp[core]:x64-windows -> 0.1.7 -- C:\Users\michaelyin\AppData\Local\vcpkg\registries\git-trees\5eb63527141d7d261b6e99945f81bf43e293cc8b
* vcpkg-cmake[core]:x64-windows -> 2023-05-04 -- C:\Users\michaelyin\AppData\Local\vcpkg\registries\git-trees\88a7058fc7fa73a9c4c99cfcae9d79e2abf87a5a
* vcpkg-cmake-config[core]:x64-windows -> 2022-02-06#1 -- C:\Users\michaelyin\AppData\Local\vcpkg\registries\git-trees\8d54cc4f487d51b655abec5f9c9c3f86ca83311f
Additional packages (*) will be modified to complete this operation.
Restored 3 package(s) from C:\Users\michaelyin\AppData\Local\vcpkg\archives in 1.2 s. Use --debug to see more details.
Installing 1/3 vcpkg-cmake-config:x64-windows...
Elapsed time to handle vcpkg-cmake-config:x64-windows: 43.9 ms
Installing 2/3 vcpkg-cmake:x64-windows...
Elapsed time to handle vcpkg-cmake:x64-windows: 51.1 ms
Installing 3/3 concurrencpp:x64-windows...
Elapsed time to handle concurrencpp:x64-windows: 214 ms
Total install time: 1.5 s
concurrencpp provides CMake targets:
# this is heuristically generated, and may not be correct
find_package(concurrencpp CONFIG REQUIRED)
target_link_libraries(main PRIVATE concurrencpp::concurrencpp)
我的测试代码,与上面的文档相同
#include "concurrencpp/concurrencpp.h"
#include <iostream>
int main() {
return 0;
}
我希望链接是自动处理的,但实际上并非如此
Error :Error (active) E1696 cannot open source file "concurrencpp/concurrencpp.h" cpp20new D:\project_CPP\C++_study\cpp20new\cpp20new\cpp20new.cpp 4
仅使用 CMake 方法,仅供参考
这似乎有效?
如果不使用 vcpkg,而只使用 CMAKE,它就可以工作了
(1)在Visual Studio中创建cmake项目 (2)在同一根下,按照指示进行 https://github.com/David-Haim/concurrencpp#building-installing-and 测试
$ git clone https://github.com/David-Haim/concurrencpp.git
$ cd concurrencpp
$ cmake -S . -B build/lib
编辑我的项目 CMake
project ("CMakeProject1")
# Add source to this project's executable.
add_executable (CMakeProject1 "CMakeProject1.cpp" "CMakeProject1.h")
include(concurrencpp/cmake/coroutineOptions.cmake)
include(FetchContent)
FetchContent_Declare(concurrencpp SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/concurrencpp")
FetchContent_MakeAvailable(concurrencpp)
target_compile_features(CMakeProject1 PRIVATE cxx_std_20)
target_link_libraries(CMakeProject1 PRIVATE concurrencpp::concurrencpp)
但我的主要问题是,如何使用 vcpkg 管理库,我可以在没有 CMAKE 的情况下做到这一点吗?
答:
所以这里有两个问题:
- 如何使用 vcpkg 管理库?
你自己已经回答了这个问题。编写一个并用它调用 vcpkg。只需手动执行与此处相同的操作:https://github.com/microsoft/vcpkg/blob/c75ff8cf3b784f67aa614552f32a6b2c7b9d8efc/scripts/buildsystems/vcpkg.cmake#L522-L536vcpkg.json
vcpkg.cmake
- 我可以在没有 CMAKE 的情况下做到这一点吗?
但是您需要自己设置所有必需的查找路径(例如包含/库路径等),这是一个繁琐的工作。
评论
让我回答自己的问题,以防有人阻止
对于 VS Studio,上面是正确的,但缺少一个额外的配置(我正在寻找的确切配置)
!.无需创建 CMAke 项目,创建一个普通的 Console 项目就足够了 2.假设您完成了上述步骤,在VS studio中,您需要在此处进行额外的配置,输入图像描述
将 VS code 配置留给其他人
评论
visual-studio
cmake
x64-windows