Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help] SDL2 addon libraries #144

Closed
ghost opened this issue Aug 15, 2020 · 3 comments
Closed

[Help] SDL2 addon libraries #144

ghost opened this issue Aug 15, 2020 · 3 comments

Comments

@ghost
Copy link

ghost commented Aug 15, 2020

I'm having a hard time using CPM with the SDL2 libraries.
I've used the ModerCppStarter repository to start this project.

Adding SDL2 works fine:

CPMAddPackage(
  NAME SDL2
  VERSION 2.0.12
  URL https://libsdl.org/release/SDL2-2.0.12.zip
)

But I can't make TTF and Mixer libraries to work as intended:


CPMAddPackage(
  NAME SDL2_ttf
  VERSION 2.0.15
  URL https://libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.15.zip
  DOWNLOAD_ONLY TRUE
)

CPMAddPackage(
  NAME SDL2_mixer
  VERSION 2.0.15
  URL https://libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.4.zip
  DOWNLOAD_ONLY TRUE
)

...

add_library(PacMan ${headers} ${sources})

target_include_directories(PacMan
  PUBLIC
    $<BUILD_INTERFACE:${SDL2_ttf_SOURCE_DIR}>
)

target_include_directories(PacMan
  PUBLIC
    $<BUILD_INTERFACE:${SDL2_mixer_SOURCE_DIR}>
)

...

target_link_libraries(PacManStandalone PacMan cxxopts SDL2 SDL2_ttf SDL2_mixer glm)

When linking the SDL2_ttf and SDL2_mixer libraries to the project, in the CMakeLists.txt file of the standalone source, I've get the following error:

[ 47%] Built target SDL2
[ 51%] Built target PacMan
[ 51%] Linking CXX executable PacMan
/usr/bin/ld: cannot find -lSDL2_ttf
/usr/bin/ld: cannot find -lSDL2_mixer
/usr/bin/ld: cannot find -lSDL2::SDL2
/usr/bin/ld: cannot find -lFreetype::Freetype

Apparently, I need to build the SDL2_ttf and SDL2_mixer linking to the SDL2 target, but I have no clue of how to do that.
Thanks in advance.

@TheLartians
Copy link
Member

TheLartians commented Aug 15, 2020

Hey,

unfortunately, SLD2 and its plugins don't seem to have great CMake support, so while it should be possible using CPM.cmake, in this case I'd recommend using the traditional approach of installing them locally on your system using a package manager such as brew or apt-get and using find_package to bring them in. Of course this will mean that others using your project will have to install the same versions of the libraries using their system package managers.

Some notes in case you still want to go forward using CPM.cmake:

  • The SDL2 library does not define required alias targets in the CMakeLists. To do so manually you can add the following after adding SDL2:
if (SDL2_ADDED)
  add_library(SDL2::SDL2 ALIAS SDL2)
endif()
  • SDL2_ttf seems to have CMake support, so it should work after removing DOWNLOAD_ONLY TRUE flag. However, it depends on FreeType, which means you should add FreeType using CPM.cmake as well if you wan't better system independence.

  • it seems that sdl2_mixer does not support CMake at all, so you will need to either create the CMake target yourself or build it outside using configure and and linking against the object files (difficult to get right).

The final script could look something like this (assuming a main.cpp in the project directory):

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

# ---- Project ----

project(Playground LANGUAGES CXX C)

# ---- Fetch CPM ----

set(CPM_DOWNLOAD_VERSION 0.27.1)
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")

if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
  message(STATUS "Downloading CPM.cmake")
  file(DOWNLOAD
    https://github.com/TheLartians/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
    ${CPM_DOWNLOAD_LOCATION}
  )
endif()

include(${CPM_DOWNLOAD_LOCATION})

# ---- Add dependencies via CPM ----
# See https://github.com/TheLartians/CPM.cmake for details and examples

CPMAddPackage(
  NAME SDL2
  VERSION 2.0.12
  URL https://libsdl.org/release/SDL2-2.0.12.zip
)

if (SDL2_ADDED)
  add_library(SDL2::SDL2 ALIAS SDL2)
endif()

CPMAddPackage(
  NAME freetype
  GIT_REPOSITORY https://github.com/aseprite/freetype2.git
  GIT_TAG VER-2-10-0
  VERSION 2.10.0
)

if (freetype_ADDED)
  add_library(Freetype::Freetype ALIAS freetype)
endif()

CPMAddPackage(
  NAME SDL2_ttf
  VERSION 2.0.15
  URL https://libsdl.org/projects/SDL_ttf/release/SDL2_ttf-2.0.15.zip
)

CPMAddPackage(
  NAME SDL2_mixer
  VERSION 2.0.15
  URL https://libsdl.org/projects/SDL_mixer/release/SDL2_mixer-2.0.4.zip
  DOWNLOAD_ONLY TRUE
)

if (SDL2_mixer_ADDED)
  # quick and dirty target definitions
  # might need extra configuration and install commands to work properly
  file(GLOB SDL2_mixer_SOURCES "${SDL2_mixer_SOURCE_DIR}/*.c")
  add_library(SDL2_mixer ${SDL2_mixer_SOURCES})
  target_link_libraries(SDL2_mixer SDL2::SDL2)
  target_include_directories(SDL2_mixer PUBLIC ${SDL2_mixer_SOURCE_DIR})
endif()

# ---- Create executable target ----

add_executable(test main.cpp)
target_link_libraries(test SDL2 SDL2_ttf SDL2_mixer)

I hope this helps you move forward with your project!

@ghost
Copy link
Author

ghost commented Aug 15, 2020

I wanted to use CPM.cmake for the convenience on multi-platform build, so I'll insist on it.

The option you mentioned on SDL2_mixer it's truly hard to get right. After doing some more research on SDL2_mixer, I've decided to switch to OpenAL.

Thanks for the help!

@TheLartians
Copy link
Member

Sounds good. I'll close the issue for now, but feel free to re-open if you run into further SDL2/CPM.cmake related problems!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant