如何使用CMake链接SDL2?

问题描述 投票:8回答:1

我使用了https://github.com/brendan-w/collector/tree/master/cmake中的.cmake文件,并将它们与CMakeLists.txt放在同一目录中,然后使用代码:

set(CMAKE_MODULE_PATH FindSDL2.cmake FindSDL2_image.cmake)
find_package(SDL2 REQUIRED)

但是我遇到了错误:

CMake Error at CMakeLists.txt:26 (find_package):
  By not providing "FindSDL2.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "SDL2", but
  CMake did not find one.

  Could not find a package configuration file provided by "SDL2" with any of
  the following names:

    SDL2Config.cmake
    sdl2-config.cmake

  Add the installation prefix of "SDL2" to CMAKE_PREFIX_PATH or set
  "SDL2_DIR" to a directory containing one of the above files.  If "SDL2"
  provides a separate development package or SDK, be sure it has been
  installed.
cmake include sdl-2 dynamic-linking
1个回答
0
投票

CMAKE_MODULE_PATH变量包含CMake模块的路径列表(FindSDL2.cmakeFindSDL2_image.cmake等是您的模块)。您应将计算机上这些模块的full路径附加到此变量:

list(APPEND CMAKE_MODULE_PATH /path/to/your/SDL2/modules)
find_package(SDL2 REQUIRED)

最近,SDL2 提供 CMake配置文件,可用于帮助CMake查找您的SDL2安装。您的错误消息对此进行了明确描述,并指出了程序包配置名称(SDL2Config.cmakesdl2-config.cmake)。要允许CMake使用此方法查找SDL2,请使用CMAKE_PREFIX_PATH

list(APPEND CMAKE_PREFIX_PATH /path/to/your/SDL2/config/files)
find_package(SDL2 REQUIRED)
© www.soinside.com 2019 - 2024. All rights reserved.