C++ vcpkg

vcpkg是用于解决C和C++库的跨平台软件包管理问题。它大大简化了在Windows、Linux和macOS上下载和配置第三方库的操作。vcpkg支持超过1900个库,并可以自动下载开源库源代码、一键安装第三方库、管理源码包的缓存和版本,并自动检查库的依赖关系并安装其依赖项。vcpkg还无缝集成Visual Studio,无需手动设置任何库相关路径,且支持全平台,包括Debug/Release、x86/x64编译,以及UWP、ARM平台的编译。

vcpkg知道你要安装哪些库是因为它可以通过命令行参数或配置文件来指定要安装的库。你可以使用vcpkg的命令行工具来列出可用的库,并选择你要安装的库。此外,vcpkg还可以通过检查项目的配置文件来自动确定所需的库,从而简化安装过程。

以下是一些使用vcpkg安装库的示例:

安装单个库:

vcpkg install <library-name>

例如,要安装Boost库,可以运行:

vcpkg install boost

安装多个库:

vcpkg install <library-name1> <library-name2> ...

例如,要同时安装Boost和OpenCV库,可以运行:

vcpkg install boost opencv

通过配置文件安装库:

你可以创建一个vcpkg.json文件,其中包含要安装的库和其版本的列表。例如:

{  
  "dependencies": [  
    {  
      "library": "boost",  
      "version": "1.78.0"  
    },  
    {  
      "library": "opencv",  
      "version": "4.5.3"  
    }  
  ]  
}

然后,运行以下命令安装配置文件中指定的库:

vcpkg install --import-config <path-to-vcpkg.json>

其中,<path-to-vcpkg.json>是vcpkg.json文件的路径。

这些示例只是使用vcpkg安装库的基础。你可以根据自己的需求和项目的要求进行定制和扩展。

以下是一个在实际项目中使用vcpkg的例子:

  • 在项目根目录下创建vcpkg.json文件,其中包含要安装的库和其版本的列表。例如:

{  
  "dependencies": [  
    {  
      "library": "boost",  
      "version": "1.78.0"  
    },  
    {  
      "library": "opencv",  
      "version": "4.5.3"  
    }  
  ]  
}
  • 在项目根目录下执行以下命令安装所需的库:

vcpkg install --import-config vcpkg.json
  • 这将使用vcpkg自动安装配置文件中指定的库,并解决任何依赖关系。

  • 将所需的库链接到项目中。这包括将库的头文件和库文件添加到项目中,并在编译时链接库。vcpkg提供了自动化的脚本和工具来帮助完成这个过程。例如,在Visual Studio中,你可以使用vcpkg的辅助项目来添加库的包含文件夹和库文件,并在项目属性中设置相应的链接选项。

这个例子只是一个简单的演示,实际项目的使用方式可能因项目的要求而有所不同。vcpkg提供了更多的功能和选项,可以根据需要进行配置和定制。

另外,可以参考C++第三方库管理工具vcpkg使用教程_令狐掌门的博客-CSDN博客

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
https://github.com/microsoft/vcpkg/blob/master/docs/examples/installing-and-using-packages.md#step-2-use Step 1: Install First, we need to know what name SQLite goes by in the ports tree. To do that, we'll run the search command and inspect the output: PS D:\src\vcpkg> .\vcpkg search sqlite libodb-sqlite 2.4.0 Sqlite support for the ODB ORM library sqlite3 3.15.0 SQLite is a software library that implements a se... If your library is not listed, please open an issue at: https://github.com/Microsoft/vcpkg/issues Looking at the list, we can see that the port is named "sqlite3". You can also run the search command without arguments to see the full list of packages. Installing is then as simple as using the install command. PS D:\src\vcpkg> .\vcpkg install sqlite3 -- CURRENT_INSTALLED_DIR=D:/src/vcpkg/installed/x86-windows -- DOWNLOADS=D:/src/vcpkg/downloads -- CURRENT_PACKAGES_DIR=D:/src/vcpkg/packages/sqlite3_x86-windows -- CURRENT_BUILDTREES_DIR=D:/src/vcpkg/buildtrees/sqlite3 -- CURRENT_PORT_DIR=D:/src/vcpkg/ports/sqlite3/. -- Downloading https://sqlite.org/2016/sqlite-amalgamation-3150000.zip... -- Downloading https://sqlite.org/2016/sqlite-amalgamation-3150000.zip... OK -- Testing integrity of downloaded file... -- Testing integrity of downloaded file... OK -- Extracting source D:/src/vcpkg/downloads/sqlite-amalgamation-3150000.zip -- Extracting done -- Configuring x86-windows-rel -- Configuring x86-windows-rel done -- Configuring x86-windows-dbg -- Configuring x86-windows-dbg done -- Build x86-windows-rel -- Build x86-windows-rel done -- Build x86-windows-dbg -- Build x86-windows-dbg done -- Package x86-windows-rel -- Package x86-windows-rel done -- Package x86-windows-dbg -- Package x86-windows-dbg done -- Performing post-build validation -- Performing post-build validation done Package sqlite3:x86-windows is installed We can check that sqlite3 was successfully installed
[i].flightNo << "\t" << flightList[i].departure << "\t" << flightList[i].destination << "\t" << flightList[i].time << "\t" << flightList[i].price << "\t" << flightList[i].left在 CMake 中使用 vcpkg 库,需要指定以下几个参数: 1. `CMAKE_TOOLCHAIN_FILE`:指SeatNum << endl; } } } //航班信息添加 void addFlightInfo() { if (currentUser.username !=定 vcpkg 提供的 cmake 工具链文件路径。 2. `CMAKE_PREFIX_PATH`:指定 vcpkg 安 "admin") { //非管理员无权添加航班信息 cout << "无权添加航班信息!" << endl装库的根目录。 下面是一个示例的 CMakeLists.txt 文件: ```cmake cmake_minimum_required(; return; } FlightInfo flight; cout << "请输入航班号:"; cin >> flight.flightNo; VERSION 3.0) # 设置项目名称 project(myproject) # 设置 C++ 标准 set(CMAKE_CXX_STANDARD cout << "请输入出发地:"; cin >> flight.departure; cout << "请输入目的地:"; cin11) set(CMAKE_CXX_STANDARD_REQUIRED True) # 指定 vcpkg 工具链文件路径 set(VCPKG_TOOLCHAIN >> flight.destination; cout << "请输入出发日期(格式:YYYY-MM-DD):"; cin >> flight.date; cout ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake CACHE STRING "Vcpkg toolchain file << "请输入出发时间(格式:HH:MM):"; cin >> flight.time; cout << "请输入总座位") # 指定 vcpkg 库的根目录 set(VCPKG_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg 数:"; cin >> flight.seatNum; flight.leftSeatNum = flight.seatNum; cout << "请输入票价 CACHE STRING "Vcpkg root directory") # 在 cmake 中引用 vcpkg 工具链文件 include(${VCPKG:"; cin >> flight.price; writeFlightInfoToFile(flight); cout << "添加成功!" << endl; } //_TOOLCHAIN}) # 设置要链接的库 find_package(Boost REQUIRED COMPONENTS system thread) # 添加可执行文件 add_executable航班信息修改 void modifyFlightInfo() { if (currentUser.username != "admin") { //非管理员无权修改(myapp main.cpp) # 链接库 target_link_libraries(myapp PRIVATE Boost::system Boost::thread) ``` 上述示航班信息 cout << "无权修改航班信息!" << endl; return; } string flightNo; 例中,我们指定了 vcpkg 的工具链文件路径和库的根目录,并在 CMakeLists.txt 文件 cout << "请输入要修改的航班号:"; cin >> flightNo; vector<FlightInfo> flightList = read中引用了 vcpkg 工具链文件,然后使用 `find_package` 命令来查找 Boost 库并链接到FlightInfoFromFile(); for (int i = 0; i < flightList.size(); i++) { if (flightList[i].可执行文件中。 如果您的项目依赖多个库,您可以在 `find_package` 命令后添加多个库的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值