Mac 上编译安装 vcpkg

1. 背景

vcpkg支持多平台,Windows、Linux以及Mac。自动集成开源第三方C 和 C++ 库,如openssl等。摘自官网:vcpkg未添加到任何环境变量,并且不依赖于Windows注册表或Visual Studio

安装使用可参考官方网站

有问题可查看github源码的issues

macOS平台的使用需求:

2. 安装命令

# 下载源码
git clone https://github.com/microsoft/vcpkg
 
# 转到安装目录
cd vcpkg
 
# mac下编译,执行脚本
./bootstrap-vcpkg.sh
# 或者
./bootstrap-vcpkg.sh -disableMetrics
  • 安装报错,具体报错及解决方法如下:
ninja: error: loading 'build.ninja': No such file or directory
  • 执行具体报错:
gushuai:vcpkg jl$ ./bootstrap-vcpkg.sh
Downloading cmake...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 
                                 Dload  Upload   Total   Spent    Left  Speed
 
100   621    0   621    0     0    529      0 --:--:--  0:00:01 --:--:--   529
 
100 27.6M  100 27.6M    0     0  1608k      0  0:00:17  0:00:17 --:--:-- 2194k
 
Downloading cmake... done.
Extracting cmake...
Extracting cmake... done.
Downloading ninja...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
 
                                 Dload  Upload   Total   Spent    Left  Speed
 
100   602    0   602    0     0    418      0 --:--:--  0:00:01 --:--:--   419
 
100 77284  100 77284    0     0  25962      0  0:00:02  0:00:02 --:--:-- 58327
 
Downloading ninja... done.
Extracting ninja...
Extracting ninja... done.
 
/Users/shandikeji/vcpkg/scripts/bootstrap.sh: line 202: [: Configured with: --prefix=/Applications/Xcode: integer expression expected
-- The C compiler identification is AppleClang 9.0.0.9000039
-- The CXX compiler identification is AppleClang 9.0.0.9000039
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/usr/bin/g++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/usr/bin/g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
 
CMake Error at CMakeLists.txt:10 (message):
  Building the vcpkg tool requires support for the C++ Filesystem TS.
  Apple clang versions 9 and below do not have support for it.
  Please install gcc6 or newer from homebrew (brew install gcc6).
  If you would like to try anyway, set VCPKG_ALLOW_APPLE_CLANG.
 
-- Configuring incomplete, errors occurred!
See also "/Users/shandikeji/vcpkg/toolsrc/build.rel/CMakeFiles/CMakeOutput.log".
ninja: error: loading 'build.ninja': No such file or directory
cp: /Users/shandikeji/vcpkg/toolsrc/build.rel/vcpkg: No such file or directory
  • 解决方法:执行命令brew install gcc6
  • 执行成功再次执行
./bootstrap-vcpkg.sh
  • 添加下面的命令到你的 shell 配置文件中,如 .bashrc 或 .zshrc,以便在任何目录都可以使用 vcpkg
export VCPKG_ROOT=/Users/leizhigang/Documents/Development/c++/hjsim/vcpkg
source $VCPKG_ROOT/scripts/buildsystems/vcpkg.sh
  • 之后就可以使用 vcpkg 安装库了,如:
复制代码vcpkg install zlib

其中 zlib 是需要安装的库名称。

3. 常用命令

  • 查找库,不加库名可以查看全部
./vcpkg search openssl
  • 安装库
./vcpkg  install openssl
  • 查看支持的架构
./vcpkg help triplet
  • 列出已经安装的开源库
./vcpkg list

4. CMake集成

为了在cmake中使用vcpkg,您需要使用以下工具链文件:

$ cmake -B [build directory] -S . -DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake
$ cmake --build [build directory]

在cmake中,您仍需通过 find_package 来使用第三方库。
为了您更好的在cmake或 VSCode CMake Tools 中使用vcpkg,需了解IDE集成方法。

若您希望在CMake中使用vcpkg,以下内容可能帮助您:

4.1. Visual Studio Code 中的 CMake Tools

将以下内容添加到您的工作区的 settings.json 中将使CMake Tools自动使用vcpkg中的第三方库:

{
  "cmake.configureSettings": {
    "CMAKE_TOOLCHAIN_FILE": "[vcpkg root]/scripts/buildsystems/vcpkg.cmake"
  }
}

4.2. Visual Studio CMake 工程中使用 vcpkg

打开CMake设置选项,将 vcpkg toolchain 文件路径在 CMake toolchain file 中:

[vcpkg root]/scripts/buildsystems/vcpkg.cmake

4.3. CLion 中使用 vcpkg

打开 Toolchains 设置
(File > Settings on Windows and Linux, CLion > Preferences on macOS),
并打开 CMake 设置 (Build, Execution, Deployment > CMake)。
最后在 CMake options 中添加以下行:

-DCMAKE_TOOLCHAIN_FILE=[vcpkg root]/scripts/buildsystems/vcpkg.cmake

遗憾的是,您必须手动将此选项加入每个项目配置文件中。

4.4. 将 vcpkg 作为一个子模块

当您希望将vcpkg作为一个子模块加入到您的工程中时,
您可以在第一个 project() 调用之前将以下内容添加到 CMakeLists.txt 中,
而无需将 CMAKE_TOOLCHAIN_FILE 传递给cmake调用。

set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake
  CACHE STRING "Vcpkg toolchain file")

使用此种方式可无需设置 CMAKE_TOOLCHAIN_FILE 即可使用vcpkg,且更容易完成配置工作。

5. 如何在Mac上安装vcpkg库并将其导入VSCode?

  1. 首先需要安装vcpkg工具,可以使用Homebrew来进行安装。在终端中运行以下命令:
brew install vcpkg
  1. 安装完vcpkg后,需要使用以下命令来安装所需的库:vcpkg install 例如,要安装OpenCV库,可以使用以下命令:
vcpkg install opencv
  1. 安装完成后,可以使用以下命令将库导入Visual Studio Code项目:
vcpkg integrate install

此命令将生成.vcpkg文件,并将路径添加到Visual Studio Code项目中。

  1. 最后,在Visual Studio Code中打开项目文件,打开终端并执行以下命令来运行项目:
g++ main.cpp -I<path-to-vcpkg>/installed/<library-name>/include -L<path-to-vcpkg>/installed/<library-name>/lib -l<library-name>

例如,如果要运行使用OpenCV库的main.cpp文件,则需要输入以下命令:

g++ main.cpp -I/usr/local/Cellar/vcpkg/2021.05.19-1/installed/x64-osx/include -L/usr/local/Cellar/vcpkg/2021.05.19-1/installed/x64-osx/lib -lopencv_core -lopencv_highgui

可以使用Tab键自动填充路径和库名称。

  • 28
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值