I use CMake’s FetchContent to pull in SDL3. Here’s a short example of my CMakeLists.txt:
option(USE_VENDORED_SDL "Download SDL3 from GitHub" ON)
if (USE_VENDORED_SDL)
message(STATUS "Using vendored SDL")
FetchContent_Declare(
SDL3
GIT_REPOSITORY https://github.com/libsdl-org/SDL
# This will be a tag once SDL3 is released
GIT_TAG origin/main
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(SDL3)
FetchContent_Declare(
SDL3_image
GIT_REPOSITORY https://github.com/libsdl-org/SDL_image
# This will be a tag once SDL3 is released
GIT_TAG origin/main
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(SDL3_image)
This setup works well with SDL2 but there are a few of roadblocks I’ve encountered with SDL3.
The first I’ve noticed is the requirement of an ASM compiler for SDL3_image which strikes me as a little strange, especially as I use SDL in environments where I cannot rely on there being one or being able to install one (University Computers).
Edit 1: It seems that the ASM Compiler is only required when set(SDL3IMAGE_VENDORED ON)
is used, making it much less of an issue. I was under the impression that this option was required to use SDL in the method described above.
The second is SDL3_image not respecting set(SDL_SHARED OFF)
. Now I understand this might be by design but it’s unclear what the correct CMake option to disable shared libs for SDL3_image would be. I tried using set(SDL3IMAGE_BUILD_SHARED_LIBS OFF)
but it seems this variable is always set to ON if the system is able to build shared libraries.
Edit 2: I went digging in StackExchange and the GitHub issues for SDL3_image and found that set(BUILD_SHARED_LIBS FALSE)
does the trick and that SDL3_image will eventually support the SDL_SHARED
and SDL_STATIC
options.
Lastly, I encounter these errors when allowing SDL3_image to build with shared libs (running the code above):
[cmake] CMake Error: install(EXPORT "libavif-config" ...) includes target "avif" which requires target "dav1d" that is not in any export set.
[cmake] CMake Error: install(EXPORT "libavif-config" ...) includes target "avif" which requires target "aom" that is not in any export set.
[cmake] CMake Error: install(EXPORT "libavif-config" ...) includes target "avif" which requires target "dav1d" that is not in any export set.
[cmake] CMake Error: install(EXPORT "libavif-config" ...) includes target "avif" which requires target "aom" that is not in any export set.
[cmake] CMake Error: install(EXPORT "libavif-config" ...) includes target "avif" which requires target "dav1d" that is not in any export set.
[cmake] CMake Error: install(EXPORT "libavif-config" ...) includes target "avif" which requires target "aom" that is not in any export set.
[cmake] CMake Error: install(EXPORT "libavif-config" ...) includes target "avif" which requires target "dav1d" that is not in any export set.
[cmake] CMake Error: install(EXPORT "libavif-config" ...) includes target "avif" which requires target "aom" that is not in any export set.
I understand that I can work around this by disabling AVIF support but I feel it should work in my usecase.
Edit 2 (cont.): I see that AVIF support should be disabled by default when using the MSVC compiler but for some reason it isn’t in my case.
For anyone looking, here is a basic CMakeLists.txt for using FetchContent with SDL3_image:
cmake_minimum_required(VERSION 3.17)
# Set the C++ Version
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(MyProject
VERSION 0.1
LANGUAGES CXX)
# Disable Shared Libs
set(BUILD_SHARED_LIBS OFF)
set(SDL_SHARED OFF)
set(SDL_STATIC ON)
# Disable AVIF Support
set(SDL3IMAGE_AVIF OFF)
# Enable FetchContent
include(FetchContent)
FetchContent_Declare(
SDL3
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG origin/main
OVERRIDE_FIND_PACKAGE
)
FetchContent_MakeAvailable(SDL3)
FetchContent_Declare(
SDL3_image
GIT_REPOSITORY https://github.com/libsdl-org/SDL_image.git
GIT_TAG origin/main
OVERRIDE_FIND_PACKAGE
)
set(SDL3IMAGE_INSTALL OFF)
set(SDL3IMAGE_BUILD_SHARED_LIBS OFF)
FetchContent_MakeAvailable(SDL3_image)
# Collect our header files
file(GLOB ENGINE_HEADER_LIST CONFIGURE_DEPENDS ${MyProject_SOURCE_DIR}/include/*.hpp)
# Set our source files
set(ENGINE_SOURCE_LIST
src/GameObject.cpp
src/Sprite.cpp
src/SpriteFactory.cpp)
# Add our library
add_library(Engine ${ENGINE_SOURCE_LIST} ${ENGINE_HEADER_LIST})
# Set the include directory
target_include_directories(Engine PUBLIC include)
# Link SDL3 and SDL3_image to our demo app
target_link_libraries(Engine PUBLIC SDL3::SDL3-static SDL3_image::SDL3_image-static)
# This is our only executable currently
add_executable(EngineDemo app/main.cpp)
target_link_libraries(EngineDemo PRIVATE Engine)
Thanks,
Mel