如何使用 cmake 在 C++ 项目中包含 Flite?

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

我必须在我的应用程序中使用 TTS 引擎,我尝试使用 Flite。 我已经安装它并从命令行对其进行了测试,但当我试图将它放入我的应用程序时,我无法让它工作。我已经在网上搜索过但没有成功,因为大多数说明都是针对 windows 或 android。我也知道 Barath-Kannan 的 c++ 包装器,但它需要 C++17,目前我无法使用它。 我在 Ubuntu 16.04.

上使用 C++11

这是我的代码:

TTSFliteManager:(使用Flite的class)

#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";
    
}
...

我在构建时遇到的错误如下:

...
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

查看文档中的示例,我了解到我缺少一些库 ("-lflite_cmu_us_kal" "-lflite_usenglish" "-lflite_cmulex" "-lflite" "-lm ") 在我的项目中。

我应该使用“find_package”吗? “include_directories”?如果是这样,让它工作的正确参数是什么?我使用的那些(Flite,FliteDll)返回一个错误,表明找不到这样的包。

我认为我需要正确配置 CMakeLists,但我不知道在那里写什么才能让项目看到 flite 库。

编辑

插件 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})

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..." )

此库不附带 pkg-config 文件或 CMake 支持,但 使用常规的 autotools 安装结构(PREFIX/include、PREFIX/lib).

这意味着你可以让 CMake 搜索它:

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)

此块在您的 CMake 搜索路径上找到 include/flite/flite.h 并使用该路径填充 FLITE_ROOT 变量。 然后我们创建一个 IMPORTED INTERFACE 目标,该目标设置了正确的 header 路径、库路径和 link 库。 如果你想让它可重复使用,上面的内容通常会放在 FindFLite.cmake 中,但现在你可以将它留在你的 CMakeLists.txt.

最后,link 你自己的目标与这个 CMake 目标:

target_link_libraries(Plugin PUBLIC FLite)

当运行第一次CMake时可能需要将CMAKE_PREFIX_PATH指向/home/user/flite.

编辑:将 SHARED IMPORTED 更改为 INTERFACE IMPORTED。前者期望找到 libFLite.so 而我已经指定了所有库依赖项。