CMake find_package cannot find SDL2

Viewed 27

Below there is the CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(sdl_test)

set(CMAKE_CXX_STANDARD 20)

list(APPEND CMAKE_PREFIX_PATH "D:/lib/SDL2_mingw")

MESSAGE(STATUS "CMAKE_PREFIX_PATH: ${CMAKE_PREFIX_PATH}")

find_package(sdl2 REQUIRED)
MESSAGE(STATUS "SDL2_INCLUDE_DIRS is ${SDL2_INCLUDE_DIRS}")
MESSAGE(STATUS "SDL2_LIBRARIES is ${SDL2_LIBRARIES}")

include_directories(${SDL2_INCLUDE_DIRS})
include_directories(${SDL2_INCLUDE_DIRS}-config-)
add_executable(sdl_test main.cpp)
target_link_libraries(sdl_test ${SDL2_LIBRARIES})

And the CMake console output is:


-- CMAKE_PREFIX_PATH: D:/lib/SDL2_mingw
-- SDL2_INCLUDE_DIRS is D://include;D://include/SDL2
-- SDL2_LIBRARIES is SDL2::SDL2
-- Configuring done
CMake Error at CMakeLists.txt:15 (add_executable):
  Target "sdl_test" links to target "SDL2::SDL2" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?


-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.

Not only the ${SDL2_LIBRARIES} (SDL::SDL) cannot be linked, ${SDL2_INCLUDE_DIRS} (D://include;D://include/SDL2) is also wrong. Facutually, it should be D:/lib/SDL2_mingw/include.

My SDL2 lib is under D:/lib/SDL2_mingw. And there does exist a SDL2Config.cmake file. I have write list(APPEND CMAKE_PREFIX_PATH "D:/lib/SDL2_mingw") in the CMakeList.

D:/lib/SDL2_mingw Directory

What's going on here?


Update:

if I enable CMake_GNUtoMS when configure CMake for SDL2, after re-build SDL2 I get both *.a and *.lib files under D:/lib/SDL2_mingw directory.

*.lib files

And then CMake can find the package. However, when compile my project, new error occurs:

mingw32-make.exe[3]: *** No rule to make target 'D:/lib/SDL2_mingw/lib/x64/SDL2.lib', needed by 'sdl_test.exe'.  Stop.
mingw32-make.exe[3]: *** Waiting for unfinished jobs....
[ 50%] Building CXX object CMakeFiles/sdl_test.dir/main.cpp.obj
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:82: CMakeFiles/sdl_test.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:89: CMakeFiles/sdl_test.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:123: sdl_test] Error 2
0 Answers
Related