MSYS2 is very confusing. When you pick "MSYS2", you are building exclusively for the MSYS2 target environment, and might not have proper compatible windows headers. When you pick "MINGW32/64", you are instead building for the normal windows environment, and get proper windows headers. But if you didn't know that, you would end up confused about why your program is not building.
It doesn't help that the package simply named "gcc" is for the MSYS2 target.
It doesn't help that the package simply named "gcc" is for the MSYS2 target.