title: Make a C++ SDL2 standalone and cross-platform-development with CMake
body: <p>My goal is to build a C++ app with SDL2 that can run on <strong>both</strong> Linux and Windows which may <strong>not have SDL2 installed</strong>. <em>(I know that there are lot of posts about it, I'll come later)</em>.</p>
  <p>So I'm going through my little project structure and I remember using CMake to make building more efficient. So I document myself a lot and conclude to this structure :</p>
  <pre><code>- app (contains the main function)
    |- main.cpp
    |- CMakeLists.txt
  - build
  - libs (contains externals libraries such as: &quot;spdlog&quot;, &quot;googletest&quot;)
  - src
    |- all the source files
    |- CMakeLists.txt
  - tests
    |- tests files
    |- CMakeLists.txt
  CMakeLists.txt
  </code></pre>
  <p><em>Structure is not the point of this discussion.</em></p>
  <p>Now that my project structure is done, I focus on my build files.</p>
  <p>Thanks to a lot of posts, I understand that I should go through <strong>static linking</strong>. (I know the cons such as recompiling statics libraries, not having last updates automatically, larger files, ...). I want to distribute my program as a &quot;folder&quot; so I excluded the solution of (package) installers. <strong>My program must be able to run with his own resources (folders).</strong> Also, remember that SDL2 came with the &quot;zlib license&quot; allowing static link.</p>
  <p>So I write my <strong>src/CMakeLists.txt</strong> :</p>
  <pre><code>set(PROJ_LIB_NAME &quot;projectlib&quot;)
  
  configure_file(
      ${CMAKE_CURRENT_SOURCE_DIR}/config/cmake_config.hpp.in 
      ${CMAKE_CURRENT_SOURCE_DIR}/config/cmake_config.hpp
  )
  
  set(PROJ_LIB_SOURCES
      core.cpp
      utils/logs_utils.cpp
      gui/gui.cpp
  )
  
  add_library(${PROJ_LIB_NAME} STATIC ${PROJ_LIB_SOURCES})
  target_include_directories(${PROJ_LIB_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
  
  ### ====================================
  ### LIBRARIES
  ### ====================================
  
  ### spdlog
  ### ====================================
  if(NOT TARGET spdlog)
      # Stand-alone build
      find_package(spdlog REQUIRED)
  endif()
  target_link_libraries(${PROJ_LIB_NAME} PRIVATE spdlog::spdlog)
  
  
  ### SDL2
  ### ====================================
  find_package(SDL2 REQUIRED)
  target_include_directories(${PROJ_LIB_NAME} PRIVATE ${SDL2_INCLUDE_DIRS})
  target_link_libraries(${PROJ_LIB_NAME} PRIVATE -static ${SDL2_LIBRARIES})
  
  </code></pre>
  <p><em>Please note the <code>-static</code> at the last line</em></p>
  <p>And my <strong>CMakeLists.txt</strong> (root):</p>
  <pre><code>cmake_minimum_required(VERSION 3.16.3 FATAL_ERROR)
  enable_testing()
  
  set(PROJ_NAME &quot;progr&quot;)
  set(PROJ_VERSION &quot;1.0.0&quot;)
  
  # set the project name
  project(${PROJ_NAME} VERSION ${PROJ_VERSION})
  
  # specify the C++ standard
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_CXX_STANDARD_REQUIRED True)
  
  # specify compiler flags
  set(CMAKE_CXX_FLAGS &quot;${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror -Wpedantic&quot;)
  set(CMAKE_CXX_FLAGS_DEBUG &quot;${CMAKE_CXX_FLAGS_DEBUG} -g&quot;)
  set(CMAKE_CXX_FLAGS_RELEASE &quot;${CMAKE_CXX_FLAGS_RELEASE} -O3&quot;)
  
  add_subdirectory(${CMAKE_SOURCE_DIR}/libs/googletest-1.11.0)
  add_subdirectory(${CMAKE_SOURCE_DIR}/libs/spdlog-1.8.5)
  add_subdirectory(${CMAKE_SOURCE_DIR}/src)
  # add_subdirectory(${CMAKE_SOURCE_DIR}/tests)
  # add_subdirectory(${CMAKE_SOURCE_DIR}/app)
  </code></pre>
  <p>Finally, the <strong>app/CMakeLists.txt</strong>:</p>
  <pre><code>set(PROJ_EXE_NAME &quot;main&quot;)
  
  add_executable(${PROJ_EXE_NAME} main.cpp)
  target_link_libraries(${PROJ_EXE_NAME} PUBLIC projectlib)
  </code></pre>
  <p>Seems good but <strong>doesn't work</strong>. If I uncomment <code>add_subdirectory(${CMAKE_SOURCE_DIR}/app)</code> to build my app, I get a bunch of undefined references coming from &quot;/usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a&quot;, &quot; ../libs/spdlog-1.8.5/libspdlogd.a&quot;, &quot;/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libSDL2.a&quot;...</p>
  <hr />
  <p><strong>What I've tried so far to make my app standalone portable:</strong></p>
  <ul>
  <li>downloading the SDL2 source code and put it in libs/, telling CMake to treat it like a subdirectory -&gt; <del>that was dumb because the source code is aiming to install SDL on your computer, not to be used as it by your project...</del> (following SDL installation guide, you can)</li>
  <li>looking for a findSDL2.cmake file -&gt; do I really need of those incomprehensible file? Also, it was good for SDL1 but not anymore for SDL2...</li>
  <li>playing with so much differents parameters in CMake since 2 days...</li>
  <li>many more that I can't remember for now...</li>
  </ul>
  <p><strong>There is not a single solution that everyone applauds.</strong></p>
  <p>I don't understand why there are no tutorials on how to achieve the goal of making a C++ SDL2 portable app even on machines where SDL2 is not installed... Everyone seems to agree that apps like Steam will definitely be installed and that it is up to them to manage SDL. What about games that don't go through Steam then? But then again I wonder if this is a good idea. As a JS developer, when I build an app with NPM dependencies, I block my packages versions so that all developers are working with the same tools and my production environment is stable. I don't understand this dynamic library logic so I'll be very happy to have explanations :)</p>
  <hr />
  <p><strong>EDIT:</strong> Forgot to mention that my project library (src/CMakeLists.txt) is building well and all goes wrong when I try to link it with my main fucntion (app/CMakeLists.txt)</p>
  <hr />
  <p><strong>EDIT 2:</strong> Have a working static link but with some others dependencies that must be downloaded... What I've done so far:</p>
  <ul>
  <li>downloaded the SDL source code and put it in my /libs folder (<a href="https://www.libsdl.org/download-2.0.php" rel="nofollow noreferrer">https://www.libsdl.org/download-2.0.php</a>) (<strong>No need</strong> to create a build directory inside and run 'make' or 'configure' commands. CMakeLists will handle this for you. Also, do not 'make install')</li>
  <li>modify my root CMakeLists.txt to add <code>add_subdirectory(${CMAKE_SOURCE_DIR}/libs/SDL2-2.0.14)</code> before building my sources</li>
  <li>modify my src/CMakeLists.txt to comment all the SDL find_package things (etc...) and add only the libraries linking. See :</li>
  </ul>
  <pre><code>### SDL2
  ### ====================================
  ## Comments
  #set(SDL2_DIR ${CMAKE_SOURCE_DIR}/libs/SDL2-2.0.14)
  #find_package(SDL2 REQUIRED)
  #target_include_directories(${PROJ_LIB_NAME} PRIVATE ${SDL2_INCLUDE_DIRS})
  #target_link_libraries(${PROJ_LIB_NAME} PRIVATE -static ${SDL2_LIBRARIES})
  ## end Comments
  
  ## the only needed link to SDL2
  target_link_libraries(${PROJ_LIB_NAME} PRIVATE SDL2main SDL2-static)
  </code></pre>
  <ul>
  <li>after that, running my app give me some errors as &quot;No available video device&quot; at the initialization of SDL but my app was building!</li>
  <li>turns out that I need some more dependencies which are installed (I guess) when you do <code>sudo apt install libsdl2-dev</code> (<strong>what we want to avoid</strong>). You can found all the dependencies here: <a href="https://github.com/libsdl-org/SDL/blob/main/docs/README-linux.md#build-dependencies" rel="nofollow noreferrer">https://github.com/libsdl-org/SDL/blob/main/docs/README-linux.md#build-dependencies</a></li>
  <li>I've installed the dependencies and... <strong>Everything works fine!</strong></li>
  </ul>
  <p>I guess my <strong>new problem</strong> now is &quot;<em>What if I try to run my app on a computer that does not have these dependencies?</em>&quot;. Asking that because I manage to compile and run my app <strong>before</strong> installing them. I conclude that my executable has the SDL code in it but not the dependencies code.</p>
  <p>I still wonder why the static link is always so demonizing when for me it allows to have control over the versions of the dependencies. I agree with the &quot;bug fixes&quot; argument, but these new versions may be incompatible with your app. In this case, you should explain to your users that you are working very hard to release a fix when your code is not causing the regression...</p>