Undefined SDL2 symbols in 1.20.2
When I try to build the latest release, with
option(WITH_EXAMPLES "Build examples" ON)
option(WITH_EXAMPLE_HEIF_VIEW "Build heif-view tool" ON)
I get a lot of undefined symbols related to SDL2. If SDL 2 is found in config mode, it should have targets that can be linked to instead. Changing the examples/CMakeLists.txt from
find_package(SDL2 NO_MODULE)
if (SDL2_FOUND)
add_executable(heif-view ${getopt_sources}
heif_view.cc
sdl.cc
sdl.hh
common.cc
common.h)
target_link_libraries(heif-view PRIVATE heif ${SDL2_LIBRARIES})
target_include_directories(heif-view PRIVATE ${libheif_SOURCE_DIR} ${SDL2_INCLUDE_DIRS})
install(TARGETS heif-view RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()
endif ()
to
if (WITH_EXAMPLE_HEIF_VIEW)
find_package(SDL2 NO_MODULE)
if (SDL2_FOUND)
add_executable(heif-view ${getopt_sources}
heif_view.cc
sdl.cc
sdl.hh
common.cc
common.h)
target_link_libraries(heif-view PRIVATE heif SDL2::SDL2 SDL2::SDL2main)
target_include_directories(heif-view PRIVATE ${libheif_SOURCE_DIR} ${SDL2_INCLUDE_DIRS})
install(TARGETS heif-view RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()
endif ()
Allowed everything to compile properly.
Can you say what environment you are trying to compile on (e.g. which Linux distro or Windows variant), which SDL you have, and what the value of SDL2_LIBRARIES is on that environment and library version?
Must be environment specific, as I had no problems building on MSYS2 w/ SDL2 (not using sdl2-compat mind you)...
As a side note, if one does target_link_libraries() using real targets instead of variables, there is no longer a need for target_include_directories() as the linked target carries that information already.
Edit: even sdl2-compat seems to define SDL2_LIBRARIES...
Let's try to switch to the target syntax and see whether it breaks anywhere...
I left out my system details, sorry about that. I doubt it matters much really, but for completeness sake I'm running redhat. The SDL version is 2.0.12. I believe there was another change I had to make in the examples - If you link against SDL::SDL and SDL::SDLMain it seems that you'll also need to change the includes from SDL.h to SDL2/SDL.h. Not a huge change, but something to be aware of. as @kmilos mentioned earlier, this actually simplifies the CMake by removing the need to have ${SDL2_INCLUDE_DIRS} in the target include directories. This same 'bug' is in libde265 as well, which is where I presume this code was pulled from.
Thanks,
Kyle B
cmake --version
cmake version 3.26.5
NAME="Red Hat Enterprise Linux"
VERSION="8.10 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.10"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.10 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8::baseos"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8"
BUG_REPORT_URL="https://issues.redhat.com/"
Thanks. Yes, I'm aware that it's the same configuration in libde265. I just wanted to wait whether anyone has build problems with the new syntax, and then copying it over to there.
For me on Ubuntu 24.04, both SDL.h and SDL2/SDL.h work. But I understand that including the directory name helps to differentiate SDL1 from SDL2.
The SDL version is 2.0.12.
That's quite old, seems the compatibility variables were added in 2.23.1...
@farindk The stickler in me noticed SDL2 devs put SDL2::SDL2main first in the list.
@kmilos Thanks. That should then be safe. Let's just wait a week to let this settle and then I'll copy it over to libde265.