通过 CMAKE 通过 VCPKG 链接 OpenVR

Linking OpenVR through VCPKG via CMAKE

我已经通过 vcpkg 安装了 OpenVR 并正在尝试通过 CMAKE link 它,问题是我不知道如何安装,也找不到任何关于如何安装的信息。

与其他安装包不同,它会告诉您如何使用 find_package 和 target_link_libraries 通过 CMAKE link,OpenVR 不会。我最初认为

find_package(openvr CONFIG REQUIRED) 
target_link_libraries(${Proj_Name} PRIVATE openvr::openvr)

可以,但我收到错误

CMake Error at C:/dev/vcpkg/vcpkg/scripts/buildsystems/vcpkg.cmake:793 (_find_package):
Could not find a package configuration file provided by "openvr" with any
of the following names:

openvrConfig.cmake
openvr-config.cmake

Add the installation prefix of "openvr" to CMAKE_PREFIX_PATH or set
"openvr_DIR" to a directory containing one of the above files.  If "openvr"
provides a separate development package or SDK, be sure it has been
installed.

通常 vcpkg.cmake 为 CMAKE 提供 config.cmake 文件但不为 OpenVR 提供,那么我如何 link 正确地使用它?

这是一个似乎有效的查找模块。 OpenVR 通过其调试库命名做了一些非常 non-standard 的事情。我在 Linux 上测试过它,它可以工作,但我现在无法轻松访问 Windows 机器。如果失败,让我看看错误消息,我可以尝试修复它。

您将像这样使用它,来自 top-level CMakeLists.txt

# ./CMakeLists.txt
cmake_minimum_required(VERSION 3.22)
project(test)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

find_package(OpenVR REQUIRED)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE OpenVR::OpenVR)

然后将下面的查找模块放入cmake/FindOpenVR.cmake:

# cmake/FindOpenVR.cmake
cmake_minimum_required(VERSION 3.22)

function(_OpenVR_find)
  include(FindPackageHandleStandardArgs)
  include(SelectLibraryConfigurations)

  ## Find the include path
  find_path(OpenVR_INCLUDE_DIR NAMES openvr.h)

  ## Determine version by scanning header
  if (OpenVR_INCLUDE_DIR)
    set(openvr_header "${OpenVR_INCLUDE_DIR}/openvr.h")

    set(re_major "^\tstatic const uint32_t k_nSteamVRVersionMajor = ([0-9]+).*;$")
    set(re_minor "^\tstatic const uint32_t k_nSteamVRVersionMinor = ([0-9]+).*;$")
    set(re_patch "^\tstatic const uint32_t k_nSteamVRVersionBuild = ([0-9]+).*;$")

    file(STRINGS "${openvr_header}" OpenVR_VERSION_MAJOR REGEX "${re_major}")
    file(STRINGS "${openvr_header}" OpenVR_VERSION_MINOR REGEX "${re_minor}")
    file(STRINGS "${openvr_header}" OpenVR_VERSION_PATCH REGEX "${re_patch}")

    string(REGEX REPLACE "${re_major}" "\1"
           OpenVR_VERSION_MAJOR "${OpenVR_VERSION_MAJOR}")
    string(REGEX REPLACE "${re_minor}" "\1"
           OpenVR_VERSION_MINOR "${OpenVR_VERSION_MINOR}")
    string(REGEX REPLACE "${re_patch}" "\1"
           OpenVR_VERSION_PATCH "${OpenVR_VERSION_PATCH}")

    if (OpenVR_VERSION_MAJOR AND OpenVR_VERSION_MINOR AND OpenVR_VERSION_PATCH)
      set(OpenVR_VERSION
          "${OpenVR_VERSION_MAJOR}.${OpenVR_VERSION_MINOR}.${OpenVR_VERSION_PATCH}")
    endif ()
  endif ()

  ## Find the library
  find_library(OpenVR_LIBRARY_RELEASE NAMES openvr_api)

  # OpenVR uses a highly non-standard additional suffix to mark debug libraries
  list(TRANSFORM CMAKE_FIND_LIBRARY_SUFFIXES APPEND ".dbg")
  find_library(OpenVR_LIBRARY_DEBUG NAMES openvr_api)

  select_library_configurations(OpenVR)

  ## Perform all the standard required, version, etc. argument checks.
  find_package_handle_standard_args(
    OpenVR
    REQUIRED_VARS OpenVR_LIBRARY OpenVR_INCLUDE_DIR
    VERSION_VAR OpenVR_VERSION
    HANDLE_VERSION_RANGE
    HANDLE_COMPONENTS
  )

  ## Create OpenVR::OpenVR imported target.
  if (OpenVR_FOUND AND NOT OpenVR::OpenVR)
    add_library(OpenVR::OpenVR UNKNOWN IMPORTED)
    target_include_directories(OpenVR::OpenVR INTERFACE "${OpenVR_INCLUDE_DIR}")
    set_target_properties(
      OpenVR::OpenVR PROPERTIES IMPORTED_LOCATION "${OpenVR_LIBRARY}"
    )

    foreach (cfg IN ITEMS RELEASE DEBUG)
      if (OpenVR_LIBRARY_${cfg})
        set_property(
          TARGET OpenVR::OpenVR APPEND PROPERTY IMPORTED_CONFIGURATIONS ${cfg}
        )
        set_target_properties(
          OpenVR::OpenVR PROPERTIES
          IMPORTED_LOCATION_${cfg} "${OpenVR_LIBRARY_${cfg}}"
        )
      endif ()
    endforeach ()
  endif ()

  ## Export whitelisted variables
  set(OpenVR_FOUND "${OpenVR_FOUND}" PARENT_SCOPE)
  set(OpenVR_VERSION "${OpenVR_VERSION}" PARENT_SCOPE)
endfunction()

_OpenVR_find()

function(_OpenVR_find)
endfunction()

function(_OpenVR_find)
endfunction()