title: Linking SDL2 with CMake
body: <p>I'm having problems linking SDL2 to my project, which probably are because I'm new to CMake, and I don't fully know how to create new projects with it. Every time and method I've tried to fix this problem has resulted in one of the following problems:</p>
  <ol>
  <li>&quot;Cannot locate WinMain@16&quot;</li>
  <li>Multiple undefined reference errors</li>
  <li>undefined reference to `SDL_main`</li>
  </ol>
  <p>I have attempted to use find_package, linking directly to files, linking to library files, following tutorials (for example <a href="https://trenki2.github.io/blog/2017/06/02/using-sdl2-with-cmake/" rel="nofollow noreferrer">https://trenki2.github.io/blog/2017/06/02/using-sdl2-with-cmake/</a>), searching for answers (most of them talk about using find_package which I can't get to work) (for example <a href="https://stackoverflow.com/questions/28395833/using-sdl2-with-cmake">Using SDL2 with CMake</a>).</p>
  <h2>Project dependencies</h2>
  <p>SDL 2.0.12, CMake 3.17.0, 7-Zip, mingw32-make, wget</p>
  <p>The project is supposed to be cross-platform, but the main devenv is Windows 10. All scripts are run from the root folder &quot;vivaria&quot;.</p>
  <h2>Project structure</h2>
  <pre><code>vivaria/
  ├── build/
  │   └── [cmake build files]
  ├── buildtools/
  │   └── SDL2/SDL2-2.0.12/lib/x86 (and x64)
  │                            ├── SDL2.lib
  │                            └── SDL2main.libs
  ├── deploy/
  ├── resources/
  ├── scripts/
  │   ├── build_windows_debug_x86.bat
  │   └── install_buildtools_windows.bat
  └── src/
      ├── CMakeLists.txt
      └── vivaria.cpp
  </code></pre>
  <h2>build_windows_debug_x86.bat</h2>
  <pre><code>cmake -G &quot;MinGW Makefiles&quot; -S .\src\ -B .\build\ -DCMAKE_BUILD_TYPE=Debug
  cd .\build\
  mingw32-make
  </code></pre>
  <h2>vivaria.cpp</h2>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &quot;SDL.h&quot;
  
  int main() {
      if (SDL_Init(SDL_INIT_VIDEO) != 0){
          std::cout &lt;&lt; &quot;Hello world&quot; &lt;&lt; std::endl;
          return 1;
      }
      
      SDL_Quit();
      return 0;
  }
  </code></pre>
  <h2>CMakeLists.txt: undefined reference to SDL_main</h2>
  <pre><code>cmake_minimum_required(VERSION 3.17)
  project(Vivaria VERSION 1.0.0)
  
  set(CMAKE_CXX_GLAGS &quot;${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32&quot;)
  # set(CMAKE_EXE_LINKER_FLAGS &quot;-static-libgcc -static-libstdc++&quot;)
  
  set(INCLUDE_DIR &quot;${CMAKE_SOURCE_DIR}/../buildtools&quot;)
  set(SDL2_DIR &quot;${INCLUDE_DIR}/SDL2/SDL2-2.0.12&quot;)
  
  set(SDL2_INCLUDE_DIRS &quot;${SDL2_DIR}/include&quot;)
  set(CMAKE_BINARY_DIR &quot;${CMAKE_SOURCE_DIR}/../deploy&quot;)
  
  # Support both 32 and 64 bit builds
  if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
      set(SDL2_LIBRARIES &quot;${SDL2_DIR}/lib/x64/SDL2main.lib;${SDL2_DIR}/lib/x64/SDL2.lib&quot;)
  else ()
      set(SDL2_LIBRARIES &quot;${SDL2_DIR}/lib/x86/SDL2main.lib;${SDL2_DIR}/lib/x86/SDL2.lib&quot;)
  endif ()
  
  # link dependencies
  include_directories(${SDL2_INCLUDE_DIRS})
  link_directories(${SDL2_LIBRARIES})
  
  # Project files and linking
  set(SOURCES vivaria.cpp)
  add_executable(${PROJECT_NAME} vivaria.cpp)
  target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})
  </code></pre>
  <p>Console output</p>
  <pre><code>F:\Koodit\Vivaria\build&gt;mingw32-make
  [ 50%] Linking CXX executable Vivaria.exe
  c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: F:/Koodit/Vivaria/src/../buildtools/SDL2/SDL2-2.0.12/lib/x86/SDL2main.lib(Win32/Release/SDL_windows_main.obj):(.text[_main_getcmdline]+0xd1): undefined reference to `SDL_main'
  collect2.exe: error: ld returned 1 exit status
  CMakeFiles\Vivaria.dir\build.make:104: recipe for target 'Vivaria.exe' failed
  mingw32-make[2]: *** [Vivaria.exe] Error 1
  CMakeFiles\Makefile2:91: recipe for target 'CMakeFiles/Vivaria.dir/all' failed
  mingw32-make[1]: *** [CMakeFiles/Vivaria.dir/all] Error 2
  Makefile:99: recipe for target 'all' failed
  mingw32-make: *** [all] Error 2
  </code></pre>
  <h2>install_buildtools_windows.bat</h2>
  <pre><code>set SDL2=SDL2-devel-2.0.12-VC.zip
  set DOWNLOAD_DIR=%cd%\buildtools
  set OUTPUT_DIR=%cd%\buildtools\SDL2
  
  wget &quot;https://libsdl.org/release/%SDL2%&quot; -P &quot;%DOWNLOAD_DIR%&quot;
  
  7z x &quot;%DOWNLOAD_DIR%\%SDL2%&quot; -y -o%OUTPUT_DIR%
  
  del /F /Q %DOWNLOAD_DIR%\%SDL2%
  </code></pre>
  <p>As you can see I'm still &quot;a bit&quot; new to CMake, but I'm trying to learn.</p>