Compiling SDL with cmake

Hello, I want to add SDL as part of my cmake build:

cmake_minimum_required(VERSION 3.14)
project(example)

set(SRC_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../src")
set(LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../lib")

add_subdirectory("${LIB_PATH}/SDL2-2.0.10" "sdl_build")

add_executable(example "${SRC_PATH}/main.c")
target_link_libraries(example SDL2)

install(TARGETS example)

Structure:

example/
  src/
    main.c
  lib/
    SDL2-2.0.10/
  cmake/
    CMakeLists.txt
build/
  libSDL2.dylib (symlink, after build)
  sdl_build/
    libSDL2-2.0.dylib (after build)
install_dir/

Build:

cd build && cmake -DCMAKE_INSTALL_PREFIX:PATH=../install_dir ../example/util && cmake --build . --target install --config Release

But it fails with:

file INSTALL cannot find "build/sdl_build/libSDL2.dylib"

Is it problem with SDL cmake or am I doing something wrong?

1 Like

Yes I have the same problem here !

Seem’s that the symlink dylib is not generated on osx sometimes

Although I don’t have a mac to test on, after installing xcode-build-tools try building sdl2.0.10 with this script (should be placed in root sdl2.0.10 folder):

#! /usr/bin/bash

set -e

[ ! -d "mac-build" ] && mkdir mac-build
pushd mac-build

cmake -DCMAKE_BUILD_TYPE=Debug \
  -DSDL_TEST=OFF -DSDL_SHARED=OFF -DSDL_STATIC=ON ..

cmake --build . -- -j $(getconf _NPROCESSORS_ONLN)

popd

You can then link to this library however way you want.