libsdl-org / SDL_mixer

An audio mixer that supports various file formats for Simple Directmedia Layer.
zlib License
432 stars 147 forks source link

build: find a package configuration file provided by "SDL2_mixer", but CMake did not find one #530

Closed jvondermarck closed 1 year ago

jvondermarck commented 1 year ago

I have this error my CMakeList.txt is loading :

CMake Error at CMakeLists.txt:6 (find_package):
  By not providing "FindSDL2_mixer.cmake" in CMAKE_MODULE_PATH this project
  has asked CMake to find a package configuration file provided by
  "SDL2_mixer", but CMake did not find one.

  Could not find a package configuration file provided by "SDL2_mixer" with
  any of the following names:

    SDL2_mixerConfig.cmake
    sdl2_mixer-config.cmake

  Add the installation prefix of "SDL2_mixer" to CMAKE_PREFIX_PATH or set
  "SDL2_mixer_DIR" to a directory containing one of the above files.  If
  "SDL2_mixer" provides a separate development package or SDK, be sure it has
  been installed.

Here is my CMakeList.txt file :

cmake_minimum_required(VERSION 3.7)

project(Pacman LANGUAGES C)

find_package(SDL2 REQUIRED)
find_package(SDL2_mixer REQUIRED)

file(GLOB_RECURSE PROJECT_SOURCES CONFIGURE_DEPENDS
        ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c
        ${CMAKE_CURRENT_SOURCE_DIR}/src/*.h)
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)

target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_INCLUDE_DIRS} ${SDL2_MIXER_INCLUDE_DIRS}})
target_compile_definitions(${PROJECT_NAME} PRIVATE "SDL_MAIN_HANDLED")
target_link_libraries(${PROJECT_NAME} PRIVATE ${SDL2_LIBRARIES} ${SDL2_MIXER_LIBRARY})

I used the command : sudo apt-get install libsdl2-mixer-dev to download SDL_Mixer. And I build with WSL Ubuntun.

madebr commented 1 year ago

What SDL2_mixer version have you installed? SDL2_mixer installs a SDL2_mixerConfig.cmake since last year, so you must use a relatively recent release. Older Ubuntu releases won't have this (by default).

jvondermarck commented 1 year ago

Hi @madebr I just did the command "sudo apt-get install libsdl2-mixer-dev" so i have no idea what version i have, how can i use the new version ?

madebr commented 1 year ago

I think it is apt show libsdl2-mixer-dev. To use a newer version, you'll need to build the library yourself.

But if all you miss is cmake support, you can easily create a FindSDL2_mixer.cmake module and add it to your cmake module path. With this approach, you don't depend on a particular project providing cmake config files. It can be very simple:

find_package(PkgConfig QUIET)
if(PKG_CONFIG_FOUND)
    pkg_check_modules(PC_SDL2_mixer QUIET SDL2_mixer)
endif()

find_library(SDL2_mixer_LIBRARY
    NAMES SDL2_mixer
    HINTS ${PC_SDL2_mixer_LIBRARY_DIRS}
)

find_path(SDL2_mixer_INCLUDE_PATH
    NAMES SDL2/SDL_mixer.h
    HINTS ${PC_SDL2_mixer_INCLUDE_DIRS}
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(SDL2_mixer
    REQUIRED_VARS
        SDL2_mixer_LIBRARY SDL2_mixer_INCLUDE_PATH
)

if(SDL2_mixer_FOUND)
    if(NOT TARGET SDL2_mixer::SDL2_mixer)
        add_library(SDL2_mixer::SDL2_mixer UNKNOWN IMPORTED)
        set_target_properties(SDL2_mixer::SDL2_mixer PROPERTIES
            IMPORTED_LOCATION "${SDL2_mixer_LIBRARY}"
            INTERFACE_INCLUDE_DIRECTORIES "${SDL2_mixer_INCLUDE_PATH}"
        )
    endif()
endif()