我用了.cmake
来自 https://github.com/brendan-w/collector/tree/master/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_MODULE_PATH
变量包含您的 CMake 模块的路径列表( FindSDL2.cmake
、 FindSDL2_image.cmake
等是您的模块)。您应该附加 已满 您机器上这些模块的路径到此变量:
list(APPEND CMAKE_MODULE_PATH /path/to/your/SDL2/modules)
find_package(SDL2 REQUIRED)
最近,SDL2 提供了一个 CMake 配置文件,可用于帮助 CMake 找到您的 SDL2 安装。您的错误消息明确描述了这一点,并说明了包配置名称(
SDL2Config.cmake
和 sdl2-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 模块文件或包配置文件本身定义的变量名。
关于cmake - 如何使用 CMake 链接 SDL2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35280618/