[Android] Target is linking to SDL2::mixer instead of SDL2_mixer

Hi all,

I am trying to compile chocolate-doom for android (as a learning exercise). I got SDL, SDL_mixer and SDL_net to compile using the CMake build scripts. When I add chocolate-doom source to the project and try to compile, it gives me the following error:
“Target “opl” links to target “SDL2::mixer” but the target was not found.”

I changed the SDL2::mixer references to SDL2_mixer in the doom CMake files, and this issue is fixed, but it doesn’t seem like the right way to resolve this issue.
I am new to CMake builds, is something missing from the build scripts? How do I resolve it the right way?

I’ve never seen that before, and looking at the cmake directory for it, the FindSDL2_mixer.cmake file does, in fact, do the following:

if(SDL2_MIXER_FOUND)
# Imported target.
add_library(SDL2::mixer UNKNOWN IMPORTED)
set_target_properties(SDL2::mixer PROPERTIES
                      INTERFACE_COMPILE_OPTIONS "${PC_SDL2_MIXER_CFLAGS_OTHER}"
                      INTERFACE_INCLUDE_DIRECTORIES "${SDL2_MIXER_INCLUDE_DIR}"
                      INTERFACE_LINK_LIBRARIES SDL2::SDL2
                      IMPORTED_LOCATION "${SDL2_MIXER_LIBRARY}")
endif()

I’ve seen multiple variations of this Find file, but this was the first time I’ve seen SDL2:: used, but that doesn’t mean anything.

Can you change the find command at line 32 of the CMakeLists.txt to say:

FIND_PACKAGE(SDL2_mixer REQUIRED)

Alternatively, can you try adding the following to the main CMakeLists.txt (right after line 33 after it runs through some find commands) and see what it says?

IF (NOT SDL_mixer_FOUND)
        MESSAGE("Couldn't find SDL_mixer")
ENDIF (NOT SDL_mixer_FOUND)

Basically, I suspect that it is somehow not finding your SDL2 library or possibly not setting a CMake variable correctly.

You are right, it is not finding the SDL libraries and include directories. I got the following errors for each SDL library.

Could NOT find SDL2 (missing: SDL2_INCLUDE_DIR SDL2_LIBRARIES)
Could NOT find SDL2_mixer (missing: SDL2_MIXER_INCLUDE_DIR SDL2_MIXER_LIBRARY)
Could NOT find SDL2_net (missing: SDL2_NET_INCLUDE_DIR SDL2_NET_LIBRARY)

any idea how I can fix it?

I the project CMakeLists.txt file I added the following lines to build SDL libs

add_subdirectory(SDL)
add_subdirectory(SDL_net)
add_subdirectory(SDL_mixer)
add_subdirectory(doom)

target_link_libraries(
main
hidapi
SDL2
SDL2_mixer
SDL2_net )

The way I handle it is by running cmake with an environment variable called CMAKE_PREFIX_PATH.

See https://cmake.org/cmake/help/latest/variable/CMAKE_PREFIX_PATH.html

Semicolon-separated list of directories specifying installation prefixes to be searched by the find_package(), find_program(), find_library(), find_file(), and find_path() commands. Each command will add appropriate subdirectories (like bin, lib, or include) as specified in its own documentation.

By default this is empty. It is intended to be set by the project.

So for my own project, to configure my project to build a Linux executable, I run the following command:

$ CMAKE_PREFIX_PATH=/home/gberardi/Projects/Tools/CustomLibs/amd64/ cmake ../..

The amd64 directory is where I keep my custom 64-bit SDL2 library headers and binaries:

$ ls ~/Projects/Tools/CustomLibs/amd64/
bin  include  lib  share

I have a different directory for 32-bit libraries.

So if you set that environment variable to point towards where you put your installed SDL2 libraries, hopefully it will work for you.

Setting the CMAKE_PREFIX_PATH in the CMakeLists.txt file didn’t work.
I added aliases to the SDL libs,

add_library(SDL2::mixer ALIAS SDL2_mixer)
add_library(SDL2::net ALIAS SDL2_net)

this resolved the original issue of

“Target “opl” links to target “SDL2::mixer” but the target was not found.”

But now I am getting another error

CMake Error: install(EXPORT “SDL2Targets” …) includes target “SDL2-static” which requires target “m” that is not in the export set.

I couldn’t find any references to “m” anywhere. Do you know what this is?

Yep, don’t do that. Either set the environment variable by running cmake with the command line example I showed you, where the variable is set in the same line as you call it, or set the environment variable globally in your development environment before you call cmake.

Offhand I didn’t, but after searching a bit, libm is for math functions.

chocolate-doom’s CMakeLists.txt has a find_package(m) line, and there is a Findm.cmake in the cmake directory.

> # Finds libm, so we can link against it for math functions.  If libm doesn't
> # exist, linking against the m target will have no effect.

I would set that to be required as well, and see if it isn’t finding it. Maybe you need to download or install libm on your system?

But looking closer at that error message, that’s not coming from chocolate-doom’s CMakeLists.txt files. It’s from SDL2’s CMakeLists.txt, which is trying to export SDL2Targets.

The SDL2 CMakeLists.txt does check for libm and requires it.

So yeah, I think you might need to ensure libm is on your system to build. I don’t know if it is having trouble because you are trying to build for Android. libm is part of the Android NDK, so you might need to make sure that you are able to find that directory, too?