Hi,
I can’t figure out how to build a basic SDL3 application
using CMake. Can anybody help?
Thanks.
Hi,
I can’t figure out how to build a basic SDL3 application
using CMake. Can anybody help?
Thanks.
Hello!
Do you have MinGW or MSVC installed? I prefer MinGW. You can download it here: https://winlibs.com/ I have installed the MinGW GCC 13.1 version because Qt 6.10 uses it. I use Win64 version without LLVM:
Or do you prefer Visual Studio? If you like MinGW you can use Qt Creator + CMake + MinGW (but MSVC can be used with Qt Creator too). You can download the Qt Creator installer separated from Qt from GitHub Releases
You can create an empty folder for your project. Create the CMakeLists.txt file inside your project folder. Create the src folder with the main.c or main.cpp inside it.
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(example-sdl3-c)
# Set the C standard
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
# Set the name of the future application (in Windows this will be app.exe,
# while for the web it would be app.js / app.wasm)
add_executable(app)
# Specify the exact location of the library configuration files
# The variable name must strictly follow the pattern: LibraryName_DIR
set(SDL3_DIR "C:/libs/SDL3-devel-3.4.2-mingw/lib/cmake/SDL3")
# Add the source code to the project
target_sources(app
PRIVATE
src/main.c
)
# Check for the presence of libraries in the system
# If they are not found, CMake will abort the configuration with an error
# REQUIRED means the package is mandatory for the build
find_package(SDL3 REQUIRED)
# Link the libraries to our application
target_link_libraries(app PRIVATE SDL3::SDL3)
You should download to download two archives: SDL3-devel-x.x.x-mingw.zip and SDL3-x.x.x-win32-x64.zip here: GitHub Release. Unzip these folder somewhere, for example, in the C:/libs folder. Add SDL3-x.x.x-win32-x64/bin to Path (here is the SDL3.dll located)
I moved the x64 version of SDL3-devel on one level up:
Change the following path for your own in the CMakeLists.txt:
set(SDL3_DIR "C:/libs/SDL3-devel-3.4.2-mingw/lib/cmake/SDL3")
Download some example for test. For example, use the official example 01-clear: clear.c. Copy this code to your src/main.c file.
Open CMD in the your project folder (where CMakeLists.txt is located) and type commands:
> cmake -G "MinGW Makefiles" -S . -B dist/win -DCMAKE_BUILD_TYPE=Debug
> cd dist\win
> cmake --build .
> app.exe
I have created three bat files to configure, built, and run EXE:
config-exe.bat
cmake -G "MinGW Makefiles" -S . -B dist/win ^
-DCMAKE_BUILD_TYPE=Debug
build-exe.bat
cd dist\win
cmake --build .
cd ..\..
run-exe.bat
dist\win\app
I can configure, build, and run EXE by these commands:
> config-exe
> build-exe
> run-exe
You can run Qt Creator and open your project in this IDE. Select File > Open File or Project... and browse to your CMakeLists.txt. You can set breakpoints in Qt Creator and debug your project in step-by-step mode.
P.S. Qt Creator requires ~500 MB but Visual Studio requires much more. Maybe be you have a lot of free space on your SSD and you do not care about the space. But just in case.