0

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

Marsevil
  • 3
  • 1
  • If you just need this library for this project, it would be worth your time to look into a package manager like Conan or vcpkg. They can integrate with cmake and download/install the dependencies for you. My personal preference is vcpkg because I can bundle it in my project as a submodule. – sweenish Nov 11 '22 at 15:54
  • @sweenish This is not the only dependency and I have to convince my coworkers to use some package manager – Marsevil Nov 11 '22 at 16:18
  • @273K Code build when I use "classic" build command `g++ main.cpp \`PKG_CONFIG_PATH=$HOME/.local/lib/pkgconfig pkg-config --cflags --libs rtaudio\` -o main` – Marsevil Nov 11 '22 at 16:44

1 Answers1

1

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.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Thx you !! Is that working for all CMake package (and so make my `include_directories(${PackageName_INCLUDE_DIRS})`/`target_link_libraries(${PROJECT_NAME} ${PackageName_LIBS})` obsolete) or is there some special configurations ? – Marsevil Nov 11 '22 at 17:32
  • The call to `find_package(RtAudio)` doesn't create variables `PackageName_INCLUDE_DIRS` and `PackageName_LIBS` , so the commands which uses them are not needed. It is dependent on specific package, whether its `find_package` creates variables/targets and with which names. – Tsyvarev Nov 11 '22 at 17:35