Link error on Apple mobile targets
I'm adding an SDL_Image demo to SDL3 Sample. When building for iOS, tvOS and visionOS I get this linker error:
Undefined symbols for architecture arm64:
"_CGImageSourceCreateImageAtIndex", referenced from:
_LoadImageFromIOStream in libSDL3_image.a[3](IMG_ImageIO.o)
_IMG_Load in libSDL3_image.a[3](IMG_ImageIO.o)
"_CGImageSourceCreateWithDataProvider", referenced from:
_Internal_isType in libSDL3_image.a[3](IMG_ImageIO.o)
_LoadImageFromIOStream in libSDL3_image.a[3](IMG_ImageIO.o)
"_CGImageSourceCreateWithURL", referenced from:
_IMG_Load in libSDL3_image.a[3](IMG_ImageIO.o)
"_CGImageSourceGetType", referenced from:
_Internal_isType in libSDL3_image.a[3](IMG_ImageIO.o)
"_UTTypeConformsTo", referenced from:
_Internal_isType in libSDL3_image.a[3](IMG_ImageIO.o)
"_kCGImageSourceTypeIdentifierHint", referenced from:
_Internal_isType in libSDL3_image.a[3](IMG_ImageIO.o)
_LoadImageFromIOStream in libSDL3_image.a[3](IMG_ImageIO.o)
"_kUTTypeGIF", referenced from:
_IMG_isGIF in libSDL3_image.a[3](IMG_ImageIO.o)
_IMG_LoadGIF_IO in libSDL3_image.a[3](IMG_ImageIO.o)
"_kUTTypeTIFF", referenced from:
_IMG_isTIF in libSDL3_image.a[3](IMG_ImageIO.o)
_IMG_LoadTIF_IO in libSDL3_image.a[3](IMG_ImageIO.o)
ld: symbol(s) not found for architecture arm64
I was able to fix it in SDL3 Sample by explicitly linking ImageIO and CoreServices:
if (APPLE)
find_library(IO_LIB ImageIO)
find_library(CS_LIB CoreServices)
target_link_libraries(${EXECUTABLE_NAME} PUBLIC ${IO_LIB} ${CS_LIB})
endif()
Is it expected for users to link these libraries? Or should SDL_Image be linking them automatically?
This should just be EMSCRIPTEN: https://github.com/Ravbug/sdl3-sample/blob/edbf595cace68d40023f30e85c4d19a7d4eae1c5/CMakeLists.txt#L14
The error is still valid: using static libraries should still work.
here
You need to put SDL3::SDL3 last.
The error is still valid: using static libraries should still work.
here You need to put
SDL3::SDL3last.
The problem is that if you statically link SDL_image you need to link all the frameworks it uses in the application, which is what @Ravbug's fix is doing. But really we shouldn't be statically linking it.
(You probably also need to put SDL3 last, but that's separate from this issue)
The problem is that if you statically link SDL_image you need to link all the frameworks it uses in the application, which is what @Ravbug's fix is doing. But really we shouldn't be statically linking it.
You're right: SDL3 should be used as a shared library.
But CMake should handle static libraries for us: it propagates these to the final shared library or executable.
This is also why you can use SDL3::SDL3-static in your windows project without having to add the various win32 import libraries.
When we do target_link_options(SDL3_image-static PRIVATE -Wl,-framework,ImageIO), it should appear in the link command of ${EXECUTABLE_NAME}.
Ah, in that case, we probably need to do something like that in the SDL_image CMakeLists.txt for those frameworks. Can you take a look?
The problem on my side continues on iOS despite the addition of the frameworks