cmake: file format not recognized

I am working on Clion(C++ 23), I got the SDL files to load perfectly but now I am struggling with SDL_image. Essentially the FindSDL_image.cmake file would not work so I read through the finder code and just directly connected to the Directory and Library. The relevant cmake file code is listed below:

set(SDL2_IMAGE_INCLUDE_DIRS “${PROJECT_SOURCE_DIR}/SDL2_image/i686-w64-mingw32/include/SDL2”)
set(SDL2_IMAGE_LIBRARY “${PROJECT_SOURCE_DIR}/SDL2_image/i686-w64-mingw32/lib/cmake/SDL2_image/sdl2_image-config.cmake”)

INCLUDE_DIRECTORIES(${SDL2_IMAGE_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${SDL2_IMAGE_LIBRARY})

This works, but unfortunately it keeps giving the errors below and I really don’t know how to even begin tackling them.
C:\Program Files\JetBrains\CLion 2023.2.1\bin\mingw\bin/ld.exe:C:/Users/thero/CLionProjects/F_Game/SDL2_image/i686-w64-mingw32/lib/cmake/SDL2_image/sdl2_image-config.cmake: file format not recognized; treating as linker script
C:\Program Files\JetBrains\CLion 2023.2.1\bin\mingw\bin/ld.exe:C:/Users/thero/CLionProjects/F_Game/SDL2_image/i686-w64-mingw32/lib/cmake/SDL2_image/sdl2_image-config.cmake:3: syntax error
collect2.exe: error: ld returned 1 exit status

You’re not setting SDL2_IMAGE_LIBRARY to an actual library, but a CMake package config, meant to read by find_package.

Here is what you can do:

  • Tell CMake where to find the package: set(SDL2_image_DIR “${PROJECT_SOURCE_DIR}/SDL2_image/i686-w64-mingw32/lib/cmake/SDL2_image")
  • Call find_package in config mode (ignores all FindSDL2_image module): find_package(SDL2_image CONFIG)
  • Link to the library target_link_libraries(${PROJECT_NAME} PRIVATE SDL2_image::SDL2_image)

Also, be careful, I noticed you were setting paths to the i686 (x86) version of the library. If you’re using the x86_64 (x64) toolchain, these libraries will not be compatible!

1 Like

Thank you so much, its finally working, and yah didn’t even notice the x86_64 (x64) mistake so you’ve saved me two headaches.