Using vcpkg and cmake with Visual studio 2019

less than 1 minute read

C++ project package manager

Comparison

  • Read thoes detail at Reddit
  • vcpkg
    • Repository spark star
      Readme Card Sparkline
  • Conan
    • Repository spark star
      Readme Card Sparkline
  • build2
    • Repository spark star
      Readme Card Sparkline

Install vcpkg

Install Windows

git clone https://github.com/microsoft/vcpkg
cd vcpkg
bootstrap-vcpkg.bat

install package

vcpkg install eigen3

Check support platform

vcpkg help triplets

Check Installed package

vcpkg list

Using vcpkg with cmake under Visual studio 2019

  • For windows Visual studio 2019
vcpkg install sqlite3:x64-windows
  • 允许全局引用
vcpkg integrate install
  • Change CMakeLists.txt
cmake_minimum_required (VERSION 3.8)

project (vs_cmake CXX)

find_package(unofficial-sqlite3 CONFIG REQUIRED)
find_package(fmt REQUIRED)
find_package(range-v3 REQUIRED)
find_package(cxxopts REQUIRED)


add_executable (vs_cmake "vs_cmake.cpp" "vs_cmake.h")
target_compile_features(vs_cmake PRIVATE cxx_std_20)
target_link_libraries(vs_cmake
  PRIVATE
    fmt::fmt
    range-v3::range-v3
    cxxopts::cxxopts
    unofficial::sqlite3::sqlite3)
  • project 指定项目名
  • find_package 指定项目依赖
  • add_executable 指定编译后的可执行程序
  • target_compile_features 指定编译版本cxx_std_20, 使用/std:c++20作为编译版本
  • target_link_libraries 为项目指定链接库

add Cmake Tool chain

  • add "cmakeToolchain": "\path\to\vcpkg\\scripts\\buildsystems\\vcpkg.cmake" at CMakeSettings.json
  • or use -DCMAKE_TOOL_CHAIN= at command line

Categories:

Updated: