tartarus's bolg tartarus's bolg
  • Linux and Unix Guide
  • CMake
  • gcc
  • gdb
  • bash
  • GNU Make
  • DDCA-ETH
  • CS106L
  • CS144
  • NJU PA
  • NJU OS(jyy)
  • C
  • C++
  • Python
  • reveal-md
  • LaTex
  • Paper Reading
  • TBD
  • Linux and Unix Guide
  • CMake
  • gcc
  • gdb
  • bash
  • GNU Make
  • DDCA-ETH
  • CS106L
  • CS144
  • NJU PA
  • NJU OS(jyy)
  • C
  • C++
  • Python
  • reveal-md
  • LaTex
  • Paper Reading
  • TBD
  • pdb

  • make

  • cmake

    • Introduction
    • Basic Intro
    • Basic 01-hello-cmake
    • Basic 02-hello-headers
    • Basic 03-static-library
    • Basic 04-shared-library
    • Basic 05-installing
    • Basic 06-build-type
    • Basic 07-complie-flags
    • Basic 08-third-party-library
    • Basic 09-compiling-with-clang
    • Basic 10-building-with-ninjia
    • Basic 11-cpp-standard
    • Intermediate sub-projects
    • Intermediate static-analysis
    • packge-management 04-conan
    • packge-management 05-vcpkg
      • 介绍
      • 安装vcpkg及所需的库
      • 构建本例
    • Offical Tutorial(未完成)
  • Linux and Unix

  • Basic_Software
  • cmake
tartarus
2023-04-18
目录

packge-management 05-vcpkg

# 介绍

vcpkg (opens new window) 是另一个 C++ 包管理器,它也支持多种构建系统,而且使用简单,比较适合我这种新手菜鸡。CS106L 使用的就是这种方法进行管理。

这里使用一个使用 vcpkg 管理库 fmt 的例子简单介绍一下 vcpkg 的使用方法,笔者对其了解也还在很浅,之后会不断补充。

本例的目录🌲:

├── CMakeLists.txt
├── main.cpp # 包含了头文件 `#include <fmt/format.h>`
1
2

# 安装 vcpkg 及所需的库

在当前目录下进行安装:

$ git clone https://github.com/Microsoft/vcpkg.git

$ cd vcpkg

$ ./bootstrap-vcpkg.sh

$ ./vcpkg install
...
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

提示

运行 ./vcpkg install ... 命令时,会有命令行提示你如何在 CMakeLists.txt 中使用该库。比如 fmt 的提示中告诉我们可以以两种方式使用 fmt library。一种方式会使用到链接库,另一种方式只使用头文件。
两者的区别可以看 How come the fmt library is not header-only? (opens new window)

这种方法对任何的库都管用,比如你想使用 boost 中的 filesystem 库,你可以使用 ./vcpkg install boost-filesystem 的方法来安装和查看使用方法

提示

vcpkg 支持的库, 安装方法,支持的平台都可以在 vcpkg Browse packages (opens new window) 中直接进行查找。

然后在 CMakeLists.txt 中修改添加该库即可进行构建:

cmake_minimum_required(VERSION 3.5)
project (vcpkg_third_party_include)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package(fmt CONFIG REQUIRED)

# Add an executable
add_executable(Main main.cpp)
target_link_libraries(Main PRIVATE fmt::fmt-header-only)
1
2
3
4
5
6
7
8
9
10
11

# 构建本例

$ mkdir build

$ cd build

$ cmake .. "-DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake"
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/lib/ccache/cc
-- Check for working C compiler: /usr/lib/ccache/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: /usr/lib/ccache/c++
-- Check for working CXX compiler: /usr/lib/ccache/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build

$ make VERBOSE=1
/usr/bin/cmake -S/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic -B/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build/CMakeFiles /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build'
make -f CMakeFiles/Main.dir/build.make CMakeFiles/Main.dir/depend
make[2]: Entering directory '/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build'
cd /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build/CMakeFiles/Main.dir/DependInfo.cmake --color=
Dependee "/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build/CMakeFiles/Main.dir/DependInfo.cmake" is newer than depender "/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build/CMakeFiles/Main.dir/depend.internal".
Dependee "/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build/CMakeFiles/Main.dir/depend.internal".
Scanning dependencies of target Main
make[2]: Leaving directory '/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build'
make -f CMakeFiles/Main.dir/build.make CMakeFiles/Main.dir/build
make[2]: Entering directory '/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build'
[ 50%] Building CXX object CMakeFiles/Main.dir/main.cpp.o
/usr/lib/ccache/c++   -isystem /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/vcpkg/installed/x64-linux/include  -std=gnu++17 -o CMakeFiles/Main.dir/main.cpp.o -c /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/main.cpp
[100%] Linking CXX executable Main
/usr/bin/cmake -E cmake_link_script CMakeFiles/Main.dir/link.txt --verbose=1
/usr/lib/ccache/c++     CMakeFiles/Main.dir/main.cpp.o  -o Main  ../vcpkg/installed/x64-linux/debug/lib/libfmtd.a 
make[2]: Leaving directory '/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build'
[100%] Built target Main
make[1]: Leaving directory '/home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build'
/usr/bin/cmake -E cmake_progress_start /home/tartarus/cmake/cmake-examples/07-package-management/E-vcpkg/i-basic/build/CMakeFiles 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

当然还可以在 ${CMAKE_SOURCE_DIR} 文件夹内运行下列命令进行构建:
cmake -B ./build -S . -DCMAKE_TOOLCHAIN_FILE="./vcpkg/scripts/buildsystems/vcpkg.cmake"
其中:
-B ./build : 指明项目构建目录
-S . : 指名源文件在当前文件夹
-DCMAKE_TOOLCHAIN_FILE="./vcpkg/scripts/buildsystems/vcpkg.cmake : 指定 CMake 中使用的工具链 (编译器,汇编器。。。)

上次更新: 12/27/2023, 8:55:47 AM
packge-management 04-conan
Offical Tutorial(未完成)

← packge-management 04-conan Offical Tutorial(未完成)→

Theme by Vdoing | Copyright © 2023-2023 tartarus | CC BY-NC-SA 4.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式