how to include Flite in C++ project with cmake?

569 views Asked by At

I've to use a TTS engine in my application and I tried to use Flite. I've installed it and tested it from command line just fine but when I tried to put it into my application I couldn't get it to work. I've already searched the web with no success since most instructions are either for windows or android. I'm also aware of the c++ wrapper by Barath-Kannan but it requires C++17 and at moment I can't use it. I'm using C++11 on Ubuntu 16.04.

Here is my code:

TTSFliteManager: (the class where Flite is used)

#include "TTSFliteManager.h"
#include </home/user/flite/include/flite.h>
#include <utils/Logger.h>
...

void
TTSFliteManager::TTSTranslate(std::string text, std::string destination)
{
    ADD_LOG_DEBUG << "TTSFliteManager::TTSTranslate()";
    cst_voice *voice;
    flite_init();

    voice = flite_voice_select(NULL);

    float secs = flite_text_to_speech(text.c_str(),voice,destination.c_str());
    if (secs == 0)
        ADD_LOG_ERROR << "TTSFliteManager::TTSTranslate() ERROR GENERATED AUDIO FILE IS EMPTY";
    
}
...

the error I get when building is the following:

...
libPlugin.so: undefined reference to "flite_voice_select"
libPlugin.so: undefined reference to "flite_text_to_speech"
libPlugin.so: undefined reference to "flite_init"
collect2: error: ld returned 1 exit status

Looking at the example in the documentation I understand I'm missing a few libraries ("-lflite_cmu_us_kal" "-lflite_usenglish" "-lflite_cmulex" "-lflite" "-lm") in my project.

Should I use "find_package"? "include_directories"? If so what are the proper parameters to get it to work? The ones I used (Flite ,FliteDll) returned an error indicating no such package could be found.

I believe I need to properly configure the CMakeLists but I've no clue what to write there to let the project see flite libraries.

EDIT

Plugin CMakeList:

set(PLUGSOURCECD_DIR "$ENV{DEV_ROOT}/Plugin/ModPlugin")
set(PLUGSOURCEMD_DIR "$ENV{DEV_ROOT}/src/")
set(PLUGSOURCEAWT_DIR "$ENV{DEV_ROOT}/AWT/sources")
set(PLUG_DIR "$ENV{DEV_ROOT}/Plugin")
include( "$ENV{CMAKE_ADDITIONAL_MODULES_DIR}/CommonRules.cmake" )

MESSAGE("Dir = ${PLUGSOURCECD_DIR}")
MESSAGE("CMAKE_SOURCE_DIR = ${CMAKE_SOURCE_DIR}")


IF(WIN32)

    IF(NOT CMAKE_DEBUG_POSTFIX)
      set(CMAKE_DEBUG_POSTFIX d)
    ENDIF()
    if(POLICY CMP0020)
      cmake_policy(SET CMP0020 NEW)
    endif()
    if(POLICY CMP0074)
      cmake_policy(SET CMP0074 NEW)
    endif()
    if(POLICY CMP0011)
      cmake_policy(SET CMP0011 NEW)
    endif()
ENDIF()

IF(CMAKE_SOURCE_DIR STREQUAL PLUGSOURCECD_DIR)

# CMake compatibility issues.
cmake_minimum_required(VERSION 2.8)
mark_as_advanced(CMAKE_BACKWARDS_COMPATIBILITY)
project(modPlugin)
#include( "Project.cmake" )

include( "Target.cmake" )

find_package(Qt5 5.8 COMPONENTS Widgets Multimedia MultimediaWidgets REQUIRED)
# The Qt5Widgets_INCLUDES also includes the include directories for
  # dependencies QtCore and QtGui
  include_directories(${Qt5Widgets_INCLUDE_DIRS})
    MESSAGE("QT Qt5Widgets_INCLUDES = ${Qt5Widgets_INCLUDE_DIRS}")
  # We need add -DQT_WIDGETS_LIB when using QtWidgets in Qt 5.
  add_definitions(${Qt5Widgets_DEFINITIONS})
set(QT_LIBRARIES 
    Qt5::Gui Qt5::Core Qt5::Multimedia Qt5::MultimediaWidgets )
MESSAGE("QT5LIB = ${QT_LIBRARIES}")
set(QT_CLILIBRARIES ${QT_LIBRARIES})

ENDIF(CMAKE_SOURCE_DIR STREQUAL PLUGSOURCECD_DIR)

# LiquidSDR
find_package(liquiddsp REQUIRED)
include_directories(${liquiddsp_INCLUDE_DIR})
MESSAGE("LiquidDSP = ${liquiddsp_LIBRARY}")
MESSAGE("LiquidDSP include = ${liquiddsp_INCLUDE_DIR}")

INCLUDE_DIRECTORIES(${PLUGSOURCECD_DIR})


set(PCDSOURCES
#list of .cpp files
...
    ${PLUGSOURCECD_DIR}/sources/TTSFliteManager.cpp
...
        )     

set(PCDHEADER
#list of .h files
...
    ${PLUGSOURCECD_DIR}/sources/TTSFliteManager.h
...
        )               

set ( UIS
lust of .ui files
...
)

set(CMAKE_AUTORCC ON)
SET(CMAKE_AUTOUIC ON)

QT5_WRAP_CPP(PLUGINTMOCCD_SOURCES ${PCDHEADER})
QT5_WRAP_UI( UI_HEADERS ${UIS} )
set(PLUGIN_DIR "${CMAKE_BINARY_DIR}/plugins")

file(MAKE_DIRECTORY ${PLUGIN_DIR})

MESSAGE("Source = ${PCDSOURCES}")
set(modPlugin_LIBRARIES modPlugin)
add_library(${modPlugin_LIBRARIES} SHARED ${PCDSOURCES} ${PLUGINTMOCCD_SOURCES} ${UI_HEADERS})

target_link_libraries( 
    ${modPlugin_LIBRARIES} 
    ${gui_LIBRARIES} 
    ${QT_LIBRARIES}  
    ${liquiddsp_LIBRARY}
)

add_custom_command(TARGET ${modPlugin_LIBRARIES} 
                   POST_BUILD
                   COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${modPlugin_LIBRARIES}> ${PLUGIN_DIR})

Project CMakeList:

# CMake compatibility issues.
cmake_minimum_required(VERSION 2.6)
mark_as_advanced(CMAKE_BACKWARDS_COMPATIBILITY)

PROJECT(SSTUDIO)

include(Target.cmake)

INCLUDE(Project.cmake)

SET(BUILD_SHARED_LIBS "true")
set(SMSSTYLEDIR "$ENV{DEV_ROOT}/styles")
IF(BUILD_SHARED_LIBS)
MESSAGE(STATUS "=====>>>>> Compile internal libraries as shared")
ELSE()
MESSAGE(STATUS "=====>>>>> Compile internal libraries as static")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
ENDIF()

IF(WIN32)
    IF(BUILD_SHARED_LIBS)
        ADD_DEFINITIONS(-DBUILD_SHARED_LIBS)
    ENDIF()
    IF(NOT CMAKE_DEBUG_POSTFIX)
        SET(CMAKE_DEBUG_POSTFIX d)
    ENDIF()

    cmake_policy(SET CMP0020 NEW)
ENDIF()

INCLUDE("$ENV{CMAKE_ADDITIONAL_MODULES_DIR}/CommonRules.cmake")

set(SSTUDIO_SOURCES
#list of .cpp files
...
)

SET(SSTUDIO_MOC_HEADERS
#list of .h files
...
)


FIND_PACKAGE(Boost 1.57.0 REQUIRED system date_time program_options filesystem chrono thread)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
IF(WIN32)
    ADD_DEFINITIONS(-DBOOST_ALL_NO_LIB)
    ADD_DEFINITIONS(-DBOOST_ALL_DYN_LINK)
    ADD_DEFINITIONS(-DBOOST_NO_RVALUE_REFERENCES)

    LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
ENDIF()

# fftw3
find_package(FFTW REQUIRED)
include_directories(${FFTW_INCLUDES})
MESSAGE(STATUS "FFTW_INCLUDES : '${FFTW_INCLUDES}'")


find_package(ZeroMQ REQUIRED)
INCLUDE_DIRECTORIES(${ZeroMQ_INCLUDE_DIRS})


#TTS Flite commented out since is not working anyway
#set(TTS_DIR "/home/user/flite")
#find_package(FliteDll REQUIRED flite_cmu_us_kal flite_usenglish flite_cmulex flite m)
#include_directories(${FliteDll_INCLUDES})


set(CMAKE_INCLUDE_CURRENT_DIR ON)

# Libsndfile
find_package(sndfile REQUIRED)
include_directories(${sndfile_INCLUDE_DIR})
MESSAGE("Libsndfile = ${sndfile_LIBRARY}")
MESSAGE("Libsndfile include = ${sndfile_INCLUDE_DIR}")


IF($ENV{QT_SELECT} STREQUAL "4")
    set(QT_QMAKE_EXECUTABLE "$ENV{QTDIR}/Bin/qmake.exe")
    find_package(Qt4 4.8.6 REQUIRED QtGui QtCore QtSql QtNetwork QtTest QtUiTools QtXml)
    include (Qt4Macros)
    include(${QT_USE_FILE})
    include_directories(${QT_INCLUDES})
    ADD_DEFINITIONS( ${QT_DEFINITIONS} )
ELSE()
    find_package(Qt5 5.8 COMPONENTS Widgets Core Network Xml Sql UiTools Multimedia MultimediaWidgets Concurrent REQUIRED)
    # The Qt5Widgets_INCLUDES also includes the include directories for dependencies QtCore and QtGui
    include_directories(${Qt5Widgets_INCLUDE_DIRS} ${Qt5Multimedia_INCLUDE_DIRS} ${Qt5Concurrent_INCLUDE_DIRS} ${Qt5Xml_INCLUDE_DIRS})

    # We need add -DQT_WIDGETS_LIB when using QtWidgets in Qt 5.
    add_definitions(${Qt5Widgets_DEFINITIONS})
    set(SSTUDIO_QT_LIBRARIES Qt5::Gui Qt5::Core Qt5::Sql Qt5::Network Qt5::UiTools Qt5::Xml Qt5::Multimedia Qt5::MultimediaWidgets)
ENDIF()

SET(CMAKE_AUTORCC ON)
SET(CMAKE_AUTOUIC ON)

# moc'ing
if(Qt4_FOUND OR QT4_FOUND)
  QT4_WRAP_CPP( SSTUDIO_MOC_SOURCES ${SSTUDIO_MOC_HEADERS} )
ELSE()
  QT5_WRAP_CPP( SSTUDIO_MOC_SOURCES ${SSTUDIO_MOC_HEADERS})
endif(Qt4_FOUND OR QT4_FOUND)

find_package(Poco REQUIRED Net NetSSL)
find_package(OpenSSL REQUIRED)

INCLUDE("Dependencies.cmake")


SET(SSTUDIO_LIBS
    ${Boost_LIBRARIES}
    ${ZeroMQ_LIBRARIES}
    ${FFTW_LIBRARIES}
    ${SSTUDIO_QT_LIBRARIES}
...
    ${modPlugin_LIBRARIES}
    ${ftpPlugin_LIBRARIES}
    ${sndfile_LIBRARIES}
#    ${FliteDll_LIBRARIES} commented out since is not working anyway
)

IF(UNIX)
    #target_link_libraries(${roofipc_LIBRARIES} ${ZeroMQ_LIBRARIES})
ENDIF()


add_executable(
    ${TARGET_NAME} 
    ${SSTUDIO_SOURCES} 
    ${SSTUDIO_MOC_SOURCES} 
)
target_link_libraries(
    ${TARGET_NAME} 
    ${SSTUDIO_LIBS} 
    ${Poco_LIBRARIES}
)


INCLUDE("$ENV{CMAKE_ADDITIONAL_MODULES_DIR}/ProductInfo.cmake")

# ----- POST-BUILD -----
#

# post build package creation
add_custom_target( pack
    COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/pack.cmake
    COMMENT "Creating package..." )

# postbuild copies
include ( copyConfig.cmake )
include ( copyScripts.cmake )

# post RUNTIME Environment Setup
add_custom_target( runtime COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/runtimeConfiguration.sh ${TARGET_NAME} COMMENT "Setup Runtime..." )
1

There are 1 answers

5
Botje On BEST ANSWER

This library does not come with a pkg-config file or CMake support, but it does use the regular autotools installation structure (PREFIX/include, PREFIX/lib).

That means you can tell CMake to search for it:

find_path(FLITE_ROOT flite/flite.h PATH_SUFFIXES include)

add_library(FLite INTERFACE IMPORTED)
target_include_directories(FLite INTERFACE ${FLITE_ROOT}/include)
target_link_directories(FLite INTERFACE ${FLITE_ROOT}/lib)
target_link_libraries(FLite INTERFACE -lflite_cmu_us_kal -lflite_usenglish -lflite_cmulex -lflite -lm)

This block finds include/flite/flite.h on your CMake search path and populates the FLITE_ROOT variable with the path. We then create an IMPORTED INTERFACE target that is set up with the correct header path, library path, and link libraries. The above would normally go in a FindFLite.cmake if you want to make it reusable, but for now you can just leave it in your CMakeLists.txt.

Finally, link your own target with this CMake target:

target_link_libraries(Plugin PUBLIC FLite)

When running CMake the first time you may need to point CMAKE_PREFIX_PATH to /home/user/flite.

EDIT: Changed SHARED IMPORTED to INTERFACE IMPORTED. The former expects to find a libFLite.so while I already specified all library dependencies.