Hello, I recently downloaded and built the source for SDL2 with CMake (Unix Makefile) on MacOS.
The changes you recently made this year have been merged with the latest version (2.0.16) as far as I know, but I am facing an issue trying to get SDL2 to work with CMake and CLion (JetBrains C++ IDE).
I posted this question in the (unofficial?) Discord, but figured I could create a post here as well since it seemed relevant.
I built SDL2 from source and have the files in this directory:
/Users/usernamehere/SDL2/SDL2-2.0.16-install
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(HelloSDL2)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(HelloSDL2 ${SOURCE_FILES})
include_directories(/Users/usernamehere/SDL2/SDL2-2.0.16-install)
set(SDL2_DIR /Users/usernamehere/SDL2/SDL2-2.0.16-install/lib/cmake/SDL2)
find_package(SDL2 REQUIRED)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARY})
Unfortunately, I am getting the following error:
CMake Error at /Users/usernamehere/SDL2/SDL2-2.0.16-install/lib/cmake/SDL2/SDL2Config.cmake:94 (get_filename_component):
get_filename_component called with incorrect number of arguments
Where the method:
get_filename_component
Is called in SDL2Config.cmake:
get_filename_component(SDL2_EXEC_PREFIX ${SDL2_LIBDIR} PATH)
I’m assuming I’ve missed some other configuration I need to add to my CMakeLists.txt, or maybe its due to where I installed SDL2 since it isn’t in the default usr/local directory.
Maybe you (or someone who finds this topic) can help me with this issue.
Update
Ok, in my particular case, I did a Debug build for SDL2. So this problem might not have occurred for others that did a Normal/Release build.
In the SDL2Config.cmake file, there is a condition that tries to replace the two variables:
sdl2implib
sdl2mainimplib
with the debug variables:
sdl2implibdbg
sdl2mainimplibdbg
However, the code was not extracting the actual path from the Debug variables using the ${…} notation.
Here is the change that fixed it for me.
if( (NOT sdl2implib) AND sdl2implibdbg ) # if we only have a debug version of the lib
set(sdl2implib ${sdl2implibdbg})
endif()
if( (NOT sdl2mainimplib) AND sdl2mainimplibdbg ) # if we only have a debug version of the lib
set(sdl2mainimplib ${sdl2mainimplibdbg})
endif()