I have successfully built simple SDL3 Hello, World! programs in both C and C++ using both clang 22.1.1. command line with Windows 11 SDK and CMake 4.3.0 with MSVC 19. A big thanks to the SDL developers and documentation writers!
Now my goal is to compile these programs to WebAssembly with Emscripten. If understand the instructions correctly, I need to first build the SDL library with emsdk
Do you want to be able to compile it, or do you just need a binary so you can link and use wasm?
I don’t have a reason to compile it but I have done it before. I might go for it if there’s no binary release online. If you have a linux machine you may have better luck building on that. I might try it tonight if you want
Hi LevoD, thanks for your offer but I have since discovered the emcc flag -sUSE_SDL=3 which apparently links to a precompiled SDL3 library, so I no longer need to compile it myself.
Thanks @8Observer8 , Following this recipe I installed with MSYS2 and after some tweaks, emcmake cmake -S vendored/SDL -B build -G “Unix Makefiles” -DSDL_TESTS:BOOL=OFF ran without errors. However, when I tried to build with cd build/; emmake make, it failed with C:/Users/xxx/dev/msys64/home/xxx/sdl3/build/CMakeFiles/SDL3-static.dir/cmake_pch.h:4:10: fatal error: ‘/home/xxx/sdl3/vendored/SDL/src/SDL_internal.h’ file not found 4 | #include “/home/xxx/sdl3/vendored/SDL/src/SDL_internal.h” | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do you try to build SDL3 library from source or do you try to build your own example? At first, you should built SDL3 library to Wasm at first.
Use the official examples that are ready to be using for Wasm. They contains SDL3-callbacks functions. You do not need to use emscripten_cancel_main_loop();.
For example, you can:
Create an empty folder for your project. For example, example-sdl3-c
Create a CMakeLists.txt file inside it with the following content:
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(hello-wasm-sdl3-c)
# Set the name of the future application (in Windows this would be app.exe, while for web it will be app.js / app.wasm)
add_executable(app)
# Set the C standard
set(CMAKE_C_STANDARD 11)
# Tell CMake where to find the SDL3 library configuration files
set(SDL3_DIR "C:/libs/SDL3-devel-3.4.0-wasm/lib/cmake/SDL3")
# Load SDL3 package settings (compilation parameters and paths to headers)
find_package(SDL3 REQUIRED)
# Link SDL3 to our application (configures linking and include paths)
target_link_libraries(app PRIVATE SDL3::SDL3)
# Add source code to the project
target_sources(app
PRIVATE
src/main.c)
Create the src/main.c file in your project folder and copy the clear.c content to your src/main.c
C:\Users\xxx\dev\SDL>emcmake --version
emcmake is a helper for cmake, setting various environment
variables so that emcc etc. are used. Typical usage:
emcmake cmake [FLAGS]
C:\Users\xxx\dev\SDL>emcmake cmake -S . -B dist -DCMAKE_INSTALL_PREFIX=“C:/Users/xxxx/dev/SDL3-devel-3.4.2-wasm” -DCMAKE_BUILD_TYPE=MinSizeRel
emcmake: no compatible cmake generator found; Please install ninja or mingw32-make, or specify a generator explicitly using -G
Excuse me! I realize why it works for me and not for you. I have MinGW in the PATH so CMake finds it automatically. I have installed the MinGW GCC 13.1 version because Qt 6.10 uses it. I use Win64 version without LLVM: https://winlibs.com/
[ 0%] Building C object CMakeFiles/SDL3-static.dir/cmake_pch.h.pch
In file included from :1:
In file included from C:/Users/xxxx/dev/SDL3/build/CMakeFiles/SDL3-static.dir/cmake_pch.h:4:
In file included from C:/Users/xxxx/dev/SDL3/vendored/SDL/src/SDL_internal.h:63:C:/Users/xxxx/dev/SDL3/vendored/SDL/include\build_config/SDL_build_config.h:54:2: error: Wrong SDL_build_config.h,
check your include path?
54 | #error Wrong SDL_build_config.h, check your include path?
This indicates a problem with the SDL path which is located at vendored\SDL. I have received this same error message while attempting different ways to build SDL3 with emsdk. Have you ever seen it?
I discovered one of the includes in my SDL repo (located in vendored/SDL) was corrupted so I restored it to SDL/src/SDL_internal.h at main · libsdl-org/SDL · GitHub Next, I deleted my build directory and ran emcmake cmake -S vendored/SDL -B build. It reported -
emcmake: cmake -S vendored/SDL -B build ‘-DCMAKE_TOOLCHAIN_FILE=C:\Users\xxx\dev\emsdk\upstream\emscripten\cmake\Modules\Platform\Emscripten.cmake’ -DCMAKE_CROSSCOMPILING_EMULATOR=C:/Users/xxx/dev/emsdk/node/22.16.0_64bit/bin/node.exe -G ‘MinGW Makefiles’ in directory C:\Users\xxx\dev\SDL3
...
-- SDL3 was configured with the following options:
--
-- Platform: Emscripten-1
-- 64-bit: FALSE
-- Compiler: C:/Users/xxx/dev/emsdk/upstream/emscripten/emcc.bat
-- Revision: SDL-3.5.0-release-3.4.0-477-ga54dd7ba4
-- Vendor:
and ran without errors. Next I ran cmake -–build build and it built libSDL.a which appears to be static library (archive containing object files .o). I only found js and wasm files in the build/test directory. Is this normal?
Finally, it works! Thanks for your patient explanation. According to the CMake docsset(SDL3_DIR "C:/libs/SDL3-devel-3.4.0-wasm/lib/cmake/SDL3") is simply assigning a value to a variable called SDL3_DIR). This variable does not appear to be used in CMakeLists.txt and I could not find any references to it in release-3.4.2 Since it is clearly needed in the build process, I assume it must be used in another script. Where is it called? I ask this because I will next attempt to use SDL_image 3.0 in a wasm program.
It is my example that creates SDL3_DIR in CMakeLists.txt:
CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(finish-hello-wasm-sdl3-c)
# Set the C standard (must be set before add_executable)
set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)
# Set the name of the future application (in Windows this would be app.exe,
# while for the web it will be app.js / app.wasm)
add_executable(app)
# Provide a hint to CMake on where to find the SDL3 library configuration files
set(SDL3_DIR "C:/libs/SDL3-devel-3.4.2-wasm/lib/cmake/SDL3")
# Load the SDL3 package settings (compilation parameters and header paths)
find_package(SDL3 REQUIRED)
# Link SDL3 to our application (configures linking and include paths)
target_link_libraries(app PRIVATE SDL3::SDL3)
# Add the source files to the executable build
target_sources(app
PRIVATE
src/main.c
)