Not understanding why CMakeLists.txt is not finding my dependency.

I’m new to C++ and CMake and trying to get started on a WASM audio synth project but having trouble linking the dependencies.

└── πŸ“cpp-wasm-audio-synth
    └── πŸ“build
        └── cmake_install.cmake
        └── Makefile
    └── click_sound.wav
    └── CMakeLists.txt
    └── main.cpp

CMakeLists.txt:

cmake_minimum_required(VERSION 3.22)

# Set the project name and version
project(RedButtonApp VERSION 1.0 LANGUAGES CXX)

# Find SDL2_mixer package
find_package(PkgConfig REQUIRED)
find_package(SDL2 REQUIRED)

set(SDL_MIXER_INCLUDE_DIRS /usr/local/Cellar/sdl2_mixer/2.8.0/include)
set(SDL_MIXER_LIBRARIES /usr/local/Cellar/sdl2_mixer/2.8.0/lib/libSDL2_mixer-2.0.0.dylib)

set(SDL_INCLUDE_DIRS /usr/local/Cellar/sdl2/2.30.6/include/SDL2)
set(SDL_LIBRARIES /usr/local/Cellar/sdl2/2.30.6/lib/libSDL2-2.0.0.dylib)
# Add executable
add_executable(RedButtonApp main.cpp)


# Link libraries
target_link_libraries(RedButtonApp ${SDL2_MIXER_LIBRARIES})

# Include directories for SDL2_mixer
include_directories(${SDL2_MIXER_INCLUDE_DIRS})
include_directories(${SDL_INCLUDE_DIRS})
link_directories(${SDL_MIXER_LIBRARIES})
link_directories(${SDL_LIBRARIES})

From Terminal, if I go brew list sdl2 I get:

>> brew list sdl2                                                               
/usr/local/Cellar/sdl2/2.30.6/bin/sdl2-config
/usr/local/Cellar/sdl2/2.30.6/include/SDL2/ (78 files)
/usr/local/Cellar/sdl2/2.30.6/lib/libSDL2-2.0.0.dylib
/usr/local/Cellar/sdl2/2.30.6/lib/cmake/ (2 files)
/usr/local/Cellar/sdl2/2.30.6/lib/pkgconfig/sdl2.pc
/usr/local/Cellar/sdl2/2.30.6/lib/ (4 other files)
/usr/local/Cellar/sdl2/2.30.6/sbom.spdx.json
/usr/local/Cellar/sdl2/2.30.6/share/aclocal/sdl2.m4
…/visual-studio-projects/cpp-wasm-audio-synth/build took 3s 
>> 

But if I run:

>> rm -rf CMakeFiles CMakeCache.txt
cmake ..
make

I get:

-- The CXX compiler identification is AppleClang 15.0.0.15000309
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found PkgConfig: /usr/local/bin/pkg-config (found version "0.29.2")
-- Configuring done (0.5s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/<username>/Documents/visual-studio-projects/cpp-wasm-audio-synth/build
[ 50%] Building CXX object CMakeFiles/RedButtonApp.dir/main.cpp.o
/Users/<username>/Documents/visual-studio-projects/cpp-wasm-audio-synth/main.cpp:2:10: fatal error: 'SDL2/SDL.h' file not found
#include <SDL2/SDL.h>
         ^~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/RedButtonApp.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/RedButtonApp.dir/all] Error 2
make: *** [all] Error 2
…/visual-studio-projects/cpp-wasm-audio-synth/build via β–³ v3.30.2 
>> 

Don’t know why it doesn’t find my dependencies and not sure how to resolve it.

Looks like you are missing linking to SDL2::SDL2 target. So instead of:

target_link_libraries(RedButtonApp ${SDL2_MIXER_LIBRARIES})

there should be:

target_link_libraries(RedButtonApp
    PRIVATE
        SDL2::SDL2
        #SDL2::SDL2main
        ${SDL2_MIXER_LIBRARIES} # is this one needed?
)

or whichever other targets you have in the /path/to/homebrew/Cellar/sdl2/2.30.6/lib/cmake/SDL2/sdl2-config.cmake in your system.

I would also recommend to use find_package() with CONFIG, since this SDL version/package seems to provide a CMake config.

1 Like