注意这不是编译时问题,这是 cmake 生成时问题。
我一直在尝试设置我的环境,以便 cmake 可以找到 SDL2。
我在 Windows 11 上,将最新版本的 SDL2(在这里)、SDL_image(在这里)和 SDL_ttf(在这里)下载到文件夹
c:\DEVLIBS\
。我为此使用了各自的 mingw 拉链。
但是无论我尝试什么,如果它不是找到 SDL2 的问题,那么无论我尝试什么,它都永远找不到 SDL2_image 或 SDL2_ttf。
我尝试使用
find_package(SDL2 REQUIRED)
和find_package(SDL2_image REQUIRED)
等,
然后
find_package(SDL2 REQUIRED PATHS "c:\DEVLIBS\")
和find_package(SDL2_image REQUIRED PATHS "c:\DEVLIBS\SDL_image")
,
然后其他一些组合改变路径或添加提示以防万一,
然后我决定尝试 https://gitlab.com/aminosbh/sdl2-cmake-modules.git
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${SDL_FIND_MODULE_PATH})
和 find_package(SDL2 REQUIRED)
和 find_package(SDL2_image REQUIRED)
等
我还尝试修改所有涉及的路径,看看是否给它一个特定文件的列表或目录会修复它,但并没有这样做。
虽然这些都不适用于 SDL2_image 或 SDL2_ttf,但最后一个至少适用于 SDL2。 最后一个也给了我一个错误
1> [CMake] By not providing "FindSDL2_image.cmake" in CMAKE_MODULE_PATH this project
1> [CMake] has asked CMake to find a package configuration file provided by
1> [CMake] "SDL2_image", but CMake did not find one.
1> [CMake]
1> [CMake] Could not find a package configuration file provided by "SDL2_image" with
1> [CMake] any of the following names:
1> [CMake]
1> [CMake] SDL2_imageConfig.cmake
1> [CMake] sdl2_image-config.cmake
1> [CMake]
1> [CMake] Add the installation prefix of "SDL2_image" to CMAKE_PREFIX_PATH or set
1> [CMake] "SDL2_image_DIR" to a directory containing one of the above files. If
1> [CMake] "SDL2_image" provides a separate development package or SDK, be sure it has
1> [CMake] been installed.
坦率地说,我不知道为什么这不起作用。我什至检查了
CMAKE_MODULE_PATH
,即使它具有一次所需的FindSDL2*.cmake
文件的所有可能路径组合,它也拒绝工作,甚至尝试从我的/cmake
文件夹中的每个SDL文件夹添加c:/DEVLIBS/
目录。我已经通过message("${CMAKE_MODULE_PATH}")
女巫证明了这一点。
更新:我目前已经走到这一步(可以找到 SDL,但是 SDL_image 和 SDL_ttf 不能)
set(SDL_FIND_MODULE_PATH "cmake/sdl2-cmake-modules" CACHE PATH "")
set(SDL2_PATH "mysdlpath" CACHE PATH "")
set(SDL2_ttf_PATH "mysdlttfpath" CACHE PATH "")
set(SDL2_image_PATH "mysdlimagepath" CACHE PATH "")
get_filename_component(SDL_FIND_MODULE_PATH ${SDL_FIND_MODULE_PATH} ABSOLUTE)
list(APPEND CMAKE_MODULE_PATH ${SDL_FIND_MODULE_PATH})
message("${CMAKE_MODULE_PATH}")
list(APPEND CMAKE_PREFIX_PATH ${SDL2_PATH} ${SDL2_ttf_PATH} ${SDL2_image_PATH})
project ("sdlcmaketest" LANGUAGES C)
#locate sdl and set its variables
find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)
find_package(SDL2_image REQUIRED)
message("!!! SDL LIBS FOUND !!! ${SDL2_image_LIBRARIES} ${SDL2_ttf_LIBRARIES} ${SDL2_LIBRARIES}")
if (NOT SDL2_image_FOUND OR NOT SDL2_ttf_FOUND OR NOT SDL2_FOUND)
message("Uhoh")
endif()
#main executable
add_executable (${PROJECT_NAME} "main.c" "main.h")
#target_include_directories(${PROJECT_NAME} PUBLIC ${SDL2_INCLUDE_DIRS} ${SDL2_ttf_INCLUDE_DIRS} ${SDL2_image_INCLUDE_DIRS})
target_include_directories(${PROJECT_NAME} PUBLIC "lib/")
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${SDL2_image_LIBRARIES})
target_link_libraries(${PROJECT_NAME} ${SDL2_ttf_LIBRARIES})