CMake 中的 FindSDL2 发生了什么?
What happened to FindSDL2 in CMake?
我在游戏中使用 SDL2。我一直在使用自定义 FindSDL2.cmake,因为标准 CMake 集中没有。不过前段时间确实出现了关于FindSDL2的帖子。示例:Reddit post.
If your cmake is new enough and it has FindSDL2.cmake file, you can do this:
find_package(SDL2 REQUIRED)
但是当我下载最新的 CMake (3.13.2) 时,它不包括 FindSDL2.cmake
。
这是怎么回事?
FindSDL2 从未出现在 CMake 中。
按照 pull request #149, SDL2 ships with a SDL2Config.cmake
, which provides a cmake package 中的拒绝原因。
find_package
的文档指出 find_package(SDL2)
的行为如下:
- 寻找
FindSDL2.cmake
,如果存在就使用它。 (模块模式)
- 否则,请使用
SDL2Config.cmake
或 sdl2-config.cmake
中的信息。 (配置模式)
简而言之,确保您的 SDL2 包已经安装了 SDL2Config.cmake
文件,并且它在您的 CMAKE_PREFIX_PATH
上。该文档列出了它在下面查找的确切路径和前缀。
为了轻松集成 SDL2 库,我开发了 cross-platform modern CMake modules 用于查找和使用 SDL2 库以及其他相关库:
- SDL2:example, game example
- SDL2_image: example
- SDL2_ttf: example
- SDL2_mixer: example
- SDL2_net
- SDL2_gfx: animation example
因此,为了集成 SDL2 库,您唯一应该做的事情是:
- 在您的项目中克隆 SDL2 CMake 模块:
git clone https://github.com/aminosbh/sdl2-cmake-modules cmake/sdl2
- 在您的主
CMakeLists.txt
中添加以下行
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)
find_package(SDL2 REQUIRED)
target_link_libraries(${PROJECT_NAME} SDL2::Main)
注意:如果CMake没有找到SDL2库(在Windows中),我们可以指定CMake选项SDL2_PATH
如下:
cmake .. -DSDL2_PATH="/path/to/sdl2"
更多详情,请阅读README.md文件。
这是 SDL2 示例和项目的列表:https://github.com/aminosbh/sdl-samples-and-projects
我在游戏中使用 SDL2。我一直在使用自定义 FindSDL2.cmake,因为标准 CMake 集中没有。不过前段时间确实出现了关于FindSDL2的帖子。示例:Reddit post.
If your cmake is new enough and it has FindSDL2.cmake file, you can do this:
find_package(SDL2 REQUIRED)
但是当我下载最新的 CMake (3.13.2) 时,它不包括 FindSDL2.cmake
。
这是怎么回事?
FindSDL2 从未出现在 CMake 中。
按照 pull request #149, SDL2 ships with a SDL2Config.cmake
, which provides a cmake package 中的拒绝原因。
find_package
的文档指出 find_package(SDL2)
的行为如下:
- 寻找
FindSDL2.cmake
,如果存在就使用它。 (模块模式) - 否则,请使用
SDL2Config.cmake
或sdl2-config.cmake
中的信息。 (配置模式)
简而言之,确保您的 SDL2 包已经安装了 SDL2Config.cmake
文件,并且它在您的 CMAKE_PREFIX_PATH
上。该文档列出了它在下面查找的确切路径和前缀。
为了轻松集成 SDL2 库,我开发了 cross-platform modern CMake modules 用于查找和使用 SDL2 库以及其他相关库:
- SDL2:example, game example
- SDL2_image: example
- SDL2_ttf: example
- SDL2_mixer: example
- SDL2_net
- SDL2_gfx: animation example
因此,为了集成 SDL2 库,您唯一应该做的事情是:
- 在您的项目中克隆 SDL2 CMake 模块:
git clone https://github.com/aminosbh/sdl2-cmake-modules cmake/sdl2
- 在您的主
CMakeLists.txt
中添加以下行
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)
find_package(SDL2 REQUIRED)
target_link_libraries(${PROJECT_NAME} SDL2::Main)
注意:如果CMake没有找到SDL2库(在Windows中),我们可以指定CMake选项SDL2_PATH
如下:
cmake .. -DSDL2_PATH="/path/to/sdl2"
更多详情,请阅读README.md文件。
这是 SDL2 示例和项目的列表:https://github.com/aminosbh/sdl-samples-and-projects