With the compiled version SDL2-2.0.9 with MinGW-w64 on Windows, the following configuration works for me:
find_package(SDL2 REQUIRED) add_executable(sdl-test ${SOURCES}) target_link_libraries(sdl-test mingw32 SDL2::SDL2main SDL2::SDL2 )
Longer explanation
Reading the SDL2Targets.cmake file, I found out that SDL2 provides several goals:
SDL2::SDL2main ( lib/libSDL2main.a )SDL2::SDL2 ( lib/libSDL2.dll.a )SDL2::SDL2-static ( lib/libSDL2-static.a )
For each of these, INTERFACE_INCLUDE_DIRECTORIES defined, which means that we do not need to manually include include_directories for SDL2.
But only when adding SDL2::SDL2main and SDL2::SDL2 as target_link_libraries not enough. The g ++ compiler may complain of a "vague reference to" WinMain "."
-lmingw32 compiler options, I found that -lmingw32 libraries -lmingw32 added before -lmingw32 . For the -lmingw32 option -lmingw32 appear in front of the SDL2 libraries, we must also specify mingw32 as the first target_link_libraries . Which will make this configuration work.
The command I used to build it:
$ mkdir build && cd build && cmake .. -G"MinGW Makefiles" && cmake --build .
The only minor problem here is the finally generated compiler options, the -lmingw32 option -lmingw32 duplicated. But since this does not affect the layout process, I have ignored it for now.
xiGUAwanOU
source share