Home > Blockchain >  How to use RtAudio as a CMake dependency
How to use RtAudio as a CMake dependency

Time:11-12

I am trying to use RtAudio in my project. I installed it by executing :

  • git clone ...
  • cmake -B target -D CMAKE_PREFIX_PATH=$HOME/.local
  • cmake --build target
  • cmake --install target

PKG_CONFIG_PATH=$HOME/.local/lib/pkgconfig pkg-config --cflags --libs rtaudio

returns

-pthread -I$HOME/.local/include/rtaudio -D__UNIX_JACK__ -D__LINUX_ALSA__ -D__LINUX_PULSE__ -D_REENTRANT -L$HOME/.local/lib -lrtaudio

But when I'm using the following CMakeLists.txt file

project(test_rtaudio)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_FIND_DEBUG_MODE ON)

add_executable(${PROJECT_NAME} main.cpp)

find_package(RtAudio REQUIRED)
message(STATUS "RtAudio include dirs : " ${RtAudio_INCLUDE_DIRS})
message(STATUS "RtAudio lib dirs :" ${RtAudio_LIBS})

include_directories(${RtAudio_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${RtAudio_LIBS})

The RtAudioConfig.cmake is found but there is no INCLUDE_DIRS or LIBS

CMake Debug Log at $HOME/.local/share/rtaudio/RtAudioConfig.cmake:26 (find_package):
  find_package considered the following paths for FindThreads.cmake:

  The file was found at

    /usr/share/cmake/Modules/FindThreads.cmake

Call Stack (most recent call first):
  CMakeLists.txt:8 (find_package)


CMake Debug Log at CMakeLists.txt:8 (find_package):
  find_package considered the following paths for FindRtAudio.cmake:

    /usr/share/cmake/Modules/FindRtAudio.cmake

  The file was not found.

    $HOME/.local/share/rtaudio/RtAudioConfig.cmake



-- RtAudio include dirs : 
-- RtAudio lib dirs :
-- Configuring done
-- Generating done
-- Build files have been written to: $HOME/Documents/project/sandbox/rtaudio/target

CMake actually find RtAudioConfig.cmake while it is looking for pthread lib.

There is no error, but as there is no include or lib files, as soon as I am trying to compile my project :

cmake --build target

FAILED: CMakeFiles/test_rtaudio.dir/main.o 
/usr/bin/c     -std=gnu  17 -MD -MT CMakeFiles/test_rtaudio.dir/main.o -MF CMakeFiles/test_rtaudio.dir/main.o.d -o CMakeFiles/test_rtaudio.dir/main.o -c $HOME/Documents/project/sandbox/rtaudio/main.cpp
$HOME/Documents/project/sandbox/rtaudio/main.cpp:47:10: erreur fatale: RtAudio.h : Aucun fichier ou dossier de ce type
   47 | #include <RtAudio.h>
      |          ^~~~~~~~~~~
ninja: build stopped: subcommand failed.

RtAudio.h is not found.

NB : I have already tried to install the lib at the default location and the same error occurs.

I am not a professional CMake user and I can't determine if there is a miss configuration in my project or a lack of configuration while building or installing RtAudio.

If somebody have a solution or any idea

CodePudding user response:

The call find_package(RtAudio) actually creates the IMPORTED target RtAudio::rtaudio, so you could simply link with that target:

find_package(RtAudio REQUIRED)

...

target_link_libraries(${PROJECT_NAME} RtAudio::rtaudio)

Not sure why they don't mention that in their documentation.


Alternatively, it is possible to use pkg-config for extract properties of the package:

find_package(PkgConfig REQUIRED)
pkg_check_modules(RtAudio REQUIRED IMPORTED_TARGET rtaudio)

...

target_link_libraries(${PROJECT_NAME} PkgConfig::RtAudio)

(More information about using pkg-config in CMake could be found in that question.

  • Related