简体   繁体   English

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

[英]how to include Flite in C++ project with cmake?

I've to use a TTS engine in my application and I tried to use Flite.我必须在我的应用程序中使用 TTS 引擎,并尝试使用 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.我已经在网上搜索过但没有成功,因为大多数说明都是针对 windows 或 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.我也知道 Barath-Kannan 的 c++ 包装器,但它需要 C++17,目前我无法使用它。 I'm using C++11 on Ubuntu 16.04.我在 Ubuntu 16.04 上使用 C++11。

Here is my code:这是我的代码:

TTSFliteManager: (the class where Flite is used) TTSFliteManager:(使用 Flite 的类)

#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.查看文档中的示例,我知道我的项目中缺少一些库(“-lflite_cmu_us_kal”“-lflite_usenglish”“-lflite_cmulex”“-lflite”“-lm”)。

Should I use "find_package"?我应该使用“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.我使用的那些(Flite,FliteDll)返回了一个错误,表明找不到这样的包。

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.我相信我需要正确配置 CMakeLists 但我不知道在那里写什么让项目看到 flite 库。

EDIT编辑

Plugin CMakeList:插件 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:项目 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..." )

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).该库没有拿出一个PKG-config文件或CMake的支持,但它使用常规的自动工具安装结构(PREFIX /包括PREFIX / lib目录)。

That means you can tell CMake to search for it:这意味着您可以告诉 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)

This block finds include/flite/flite.h on your CMake search path and populates the FLITE_ROOT variable with the path.此块在您的 CMake 搜索路径上查找include/flite/flite.h并使用路径填充 FLITE_ROOT 变量。 We then create an IMPORTED INTERFACE target that is set up with the correct header path, library path, and link libraries.然后我们创建一个 IMPORTED INTERFACE 目标,它设置了正确的头路径、库路径和链接库。 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.如果你想让它可重用,上面通常会放在FindFLite.cmake ,但现在你可以把它留在你的 CMakeLists.txt 中。

Finally, link your own target with this CMake target:最后,将您自己的目标与此 CMake 目标链接起来:

target_link_libraries(Plugin PUBLIC FLite)

When running CMake the first time you may need to point CMAKE_PREFIX_PATH to /home/user/flite .第一次运行 CMake 时,您可能需要将CMAKE_PREFIX_PATH指向/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.前者希望找到一个 libFLite.so 而我已经指定了所有库依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM