How Can You cmake an SDL2 Project with Visual Studio Code?
- Ananth Chellappa
- Nov 7, 2020
- 1 min read
The superb LazyFoo tutorials will tell you all you need to know about SDL2. In fact, I thinking of whacking out the Ray Tracer challenge using SDL2..
But, while I was still in the throes of the Udacity C++ Nanodegree, I found that I could not get the Snake game to run on WSL2 - something Microsoft probably needs to fix.
And then? LazyFoo's stuff is all based on Visual Studio and he has no clue about cmake.
Try loading the repo (from Udacity) with Visual Studio and you get a bunch of unintelligible errors.
Enter Akram Abdeselem of Algeria :
1. Install Cmake for windows. Update Path as necessary
2. Install MinGW-w64 from http://mingw-w64.org/doku.php/download/mingw-builds. Add the bin folder to Path
3. From https://www.libsdl.org/download-2.0.php, get SDL2 devel version for mingw (a tar.gz that you'll have to decompress and untar into a location of your choice - make it C:/SDL2-2.0.12 so that the other steps here are valid :)
4. In VS Code, install the Cmake Tools and C/C++ addons : https://code.visualstudio.com/docs/cpp/cmake-linux
5. Update the CMakeLists.txt taking into account the location of mingw and SDL2 :
# hier ist der CMakeLists.txt :
cmake_minimum_required(VERSION 3.7)
add_definitions(-std=c++17)
set(CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS, "${CXX_FLAGS}")
project(SDL2Test)
# from Akram
set(CMAKE_CXX_STANDARD_LIBRARIES "-static-libgcc -static-libstdc++ -lwsock32 -lws2_32 ${CMAKE_CXX_STANDARD_LIBRARIES}")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
# from Akram
list(APPEND CMAKE_PREFIX_PATH C:/SDL2-2.0.12/x86_64-w64-mingw32/lib/cmake/SDL2)
find_package(SDL2 REQUIRED)
# from Akram (edit)
include_directories(${SDL2_INCLUDE_DIRS} C:/SDL2-2.0.12/x86_64-w64-mingw32/include/SDL2)
# from Akram
link_directories(C:/SDL2-2.0.12/x86_64-w64-mingw32/lib/)
add_executable(SnakeGame src/main.cpp src/game.cpp src/controller.cpp src/renderer.cpp src/snake.cpp)
string(STRIP ${SDL2_LIBRARIES} SDL2_LIBRARIES)
target_link_libraries(SnakeGame ${SDL2_LIBRARIES})
Comments