Link RtAudio into another project using CMakeLists

971 Views Asked by At

I'm trying to use RtAudio to generate streaming audio. However, when I try to include "RtAudio.h" file, the program always complain something weird. My project structure looks like:

  • Project
    • rtaudio
      • CMakeList1
      • RtAudio.h
    • demos
      • main.cpp
      • CMakeList2
    • CMakeList

Basically, the folder rtaudio is the repository I installed from here and the CMakeList1 is also from there.

CMakeList under the main folder:

cmake_minimum_required(VERSION 2.8)
# Add -Wall and -Wextra. Also,
# treat C/C++ warnings as errors if -DADM_FATAL_WARNINGS=ON.
include (cmake/FatalWarnings.cmake)
ADM_EXTRA_WARNINGS()
add_subdirectory(demos)

CMakeList2:

project(malos_service C CXX)
cmake_minimum_required(VERSION 2.8)
add_definitions(-std=c++11)
FIND_LIBRARY(WIRINGPI_LIB NAMES wiringPi) 
....

set(AUDIO_LIBRARY_FOUND OFF)
find_path(RTAUDIO_HEADER_PATH "RtAudio.h" HINTS ../rtaudio)
if(RTAUDIO_HEADER_PATH)
    message(STATUS "Compiling provided rtaudio-library!")
    add_subdirectory(../rtaudio rtaudio)
    add_definitions(-DRTAUDIO_HEADER="../rtaudio/RtAudio.h")
    set(AUDIO_LIBRARY_FOUND ON)
endif()
if (NOT AUDIO_LIBRARY_FOUND)
    message(SEND_ERROR "no supported library")
endif()

# Check if any audio-library was added
if(NOT AUDIO_LIBRARY_FOUND)
    message(SEND_ERROR "No supported audio-library found!")
endif()
....


add_executable(...)

To compile these, under Project folder,

mkdir build 
cd build
cmake ..
make

In main, I just add:

#include "../rtaudio/RtAudio.h"

After I compile make, I will get a lot of error:

In file included from ~/Project/demos/main.cpp:7:0:
/home/pi/Downloads/Project/demos/../rtaudio/RtAudio.h:729:5: error: expected identifier before numeric constant
     OUTPUT,
     ^
/home/pi/Downloads/Project/demos/../rtaudio/RtAudio.h:729:5: error: expected ‘}’ before numeric constant
/home/pi/Downloads/Project/demos/../rtaudio/RtAudio.h:729:5: error: expected unqualified-id before numeric constant
In file included from /home/pi/Downloads/Project/demos/main.cpp:25:0:
/home/pi/Downloads/Project/demos/../rtaudio/RtAudio.h: In member function ‘bool RtApi::isStreamOpen() const’:
/home/pi/Downloads/Project/demos/../rtaudio/RtAudio.h:709:44: error: ‘stream_’ was not declared in this scope
   bool isStreamOpen( void ) const { return stream_.state != STREAM_CLOSED; }
...

*PS: if I directly cmake rtaudio, the program under rtaudio is all working, which means the error should not comes from rtaudio * (Hopefully)

Is the way I cmake has problem? Is anybody know how to fix this?

1

There are 1 best solutions below

1
On

The reason you are getting all of those errors is because you aren't compiling the source code from RtAudio with your project. you need to copy the RtAudio.cpp file into your project. It appears you are using a raspberry pi, so really all you need to do to compile your project is to set up your files like this:

- Project
  - RtAudio.h
  - RtAudio.cpp
  - main.cpp

then to compile, you will want to run

g++ main.cpp RtAudio.cpp -o run

that's it! assuming that you have the dependencies of RtAudio installed, it should compile without issue.