如何使用CMake链接SDL2?

时间:2016-02-08 22:31:08

标签: cmake include sdl-2 dynamic-linking

我使用了来自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.

1 个答案:

答案 0 :(得分: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)

一旦CMake找到该程序包,它将定义一些SDL2_*变量,这些变量可以在您的CMake项目中使用:

...

add_executable(MySDL2Executable main.cpp)
include_directories(MySDL2Executable PRIVATE ${SDL2_INCLUDE_DIRS})
target_link_libraries(MySDL2Executable PRIVATE ${SDL2_LIBRARIES})

请注意,这些变量名称可能会根据您的SDL2版本而有所不同。有关定义的SDL2_*变量名称的列表,请查阅CMake模块文件或软件包配置文件本身。