Trouble building SDL2 project on MSYS2 using CMake

I have an Android SDL2 project and I want to build it for Windows using MSYS2. Here is my CMakeLists:

cmake_minimum_required(VERSION 3.6)
project(sdlproject)

file(GLOB_RECURSE SDLPROJECT_SRC CONFIGURE_DEPENDS "src/*.h" "src/*.cpp")
add_executable(sdlproject ${SDLPROJECT_SRC})

set(SDL2_DISABLE_INSTALL OFF)
set(SDL2_IMAGE_INSTALL OFF)
set(SDL2_MIXER_INSTALL OFF)

add_subdirectory(SDL2)
add_subdirectory(SDL2_image)
add_subdirectory(SDL2_mixer)

include_directories(src)

if(TARGET SDL2main)
    target_link_libraries(sdlproject SDL2main)
endif()
target_link_libraries(sdlproject SDL2 SDL2_image SDL2_mixer)

After building it, copying SDL2.dll, SDL2_image.dll, SDL2_mixer.dll into build folder and running executable I get the following error:

изображение

Also when generating makefiles with cmake -G "MinGW Makefiles" it throws following error:

CMake Error: install(EXPORT "SDL2ImageExports" ...) includes target "SDL2_image" which requires target "SDL2" that is not in any export set.
CMake Error: install(EXPORT "SDL2MixerTargets" ...) includes target "SDL2_mixer" which requires target "SDL2" that is not in any export set.

But when re-running this command without cleaning build folder the error disappears.

_ZNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEEC1Ev is not SDL API, I’d guess you still have to copy runtime(libstdc++6.dll etc) and/or having incorrect compiler setup. Tools like Dependencies( GitHub - lucasg/Dependencies: A rewrite of the old legacy software "depends.exe" in C# for Windows devs to troubleshoot dll load dependencies issues. ) might be useful to check which DLLs are linked against the .exe.

Thanks, making runtime libraries static solved the problem:

target_link_options(sdlproject PRIVATE -static-libgcc -static-libstdc++)

But this strange error still persists:

CMake Error: install(EXPORT "SDL2ImageExports" ...) includes target "SDL2_image" which requires target "SDL2" that is not in any export set.
CMake Error: install(EXPORT "SDL2MixerTargets" ...) includes target "SDL2_mixer" which requires target "SDL2" that is not in any export set.

Did you mean set(SDL2_DISABLE_INSTALL ON) to disable installation? In addition, options around here recently received change on cmake: cannot install SDL2_mixer when SDL2 is also built but not inst… · libsdl-org/SDL_mixer@6abf246 · GitHub / cmake: cannot install SDL2_image when SDL2 is also built but not inst… · libsdl-org/SDL_image@44354bc · GitHub and they’re now called SDL2MIXER_... ; after these changes, just saying -DSDL2_DISABLE_INSTALL=ON should be enough to disable installation.

This didn’t help. Also updating SDL to latest version and setting only SDL2_DISABLE_INSTALL=ON didn’t fix this error.

UPD: setting these flags fixed the error:

set(SDL2_DISABLE_INSTALL ON)
set(SDL2IMAGE_INSTALL OFF)
set(SDL2MIXER_INSTALL OFF)

Look like just setting SDL2_DISABLE_INSTALL=ON does not completely disable the installation.