无法使用cmakelists在项目中连接mpg123和ao

时间:2015-10-29 12:38:04

标签: c++ directory cmake mp3 libraries

我正在使用下面的cmakelists.txt,它存在于我项目的顶层文件夹中,我正在尝试将mpg123和ao连接到我的项目。在源文件夹的一个.cpp文件中,我添加了一个带mpg123和ao的代码来播放歌曲。可以使用以下行编译此代码:

g ++ mpg.cpp -lmpg123 -lao -o mpg

我还添加了这一行: target_link_libraries(emotime ${LIBAO_LIBRARIES} ${MPG123_LIBRARIES}) 在我的源文件夹中存在的cmakelists中。当我正在运行时,我会收到错误,例如"未定义对ao_initialize'" and "undefined reference to的引用mpg123_init'"在这段代码中:

    int playaudio(int  trackid)
{

char * traklink="";
int tid=trackid;
if (tid==1){
    traklink= "/home/mixa/tutti_frutti.wav";
    }
else if (tid==2){
    traklink= "/home/mixa/karavi.wav";
    }
else if (tid==3){
    traklink= "/home/mixa/timon.wav";
    }
else if (tid==4){
    traklink= "/home/mixa/hippo.wav";
    }
else{
    traklink= "/home/mixa/nanourisma.wav";
    }
mpg123_handle *mh;
    unsigned char *buffer;
    size_t buffer_size;
    size_t done;
    int err;

    int driver;
    ao_device *dev;

    ao_sample_format format;
    int channels, encoding;
    long rate;

   /* if(argc < 2)
        exit(0);
*/
    /* initializations */
    ao_initialize();
    driver = ao_default_driver_id();
    mpg123_init();
    mh = mpg123_new(NULL, &err);
    buffer_size = mpg123_outblock(mh);
    buffer = (unsigned char*) malloc(buffer_size * sizeof(unsigned char));

    /* open the file and get the decoding format */
    //mpg123_open(mh,traklink);
    mpg123_open(mh,traklink);
     mpg123_getformat(mh, &rate, &channels, &encoding);

    /* set the output format and open the output device */
   format.bits = mpg123_encsize(encoding) * BITS;
    format.rate = rate;
    format.channels = channels;
    format.byte_format = AO_FMT_NATIVE;
    format.matrix = 0;
    dev = ao_open_live(driver, &format, NULL);

    /* decode and play */
    while (mpg123_read(mh, buffer, buffer_size, &done) == MPG123_OK)
        //ao_play(dev, buffer, done);
        ao_play(dev, (char*)buffer, done);
    /* clean up */
    free(buffer);
    ao_close(dev);
    mpg123_close(mh);
    mpg123_delete(mh);
    mpg123_exit();
    ao_shutdown();
//system("mpg123 -q traklink");


 // return 0;
} 

void *threading (void *trackid)
{
long tid=(long)trackid;
cout<<"sound plays on:Thread id, "<<tid<<endl;
playaudio(tid);
pthread_exit(NULL);
}

cmakelists:

cmake_minimum_required(VERSION 2.8)

project(emotime)

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

FIND_PATH( MPG123_INCLUDE_DIR1 
  NAMES mpg123.h 
  PATH_SUFFIXES include
  PATHS
  ~/Library/Frameworks
  /Library/Frameworks
  /usr/local
  /usr
  /sw # Fink
  /opt/local # DarwinPorts
  /opt/csw # Blastwave
  /opt
  extern/mpg123/ports/MSVC++
  extern/mpg123/ports/Xcode
)

IF( MPG123_INCLUDE_DIR1 )
  SET( MPG123_INCLUDE_DIRS ${MPG123_INCLUDE_DIRS} ${MPG123_INCLUDE_DIR1} )
ENDIF( MPG123_INCLUDE_DIR1 )

# Include dir (May not be necessary on all platforms)
FIND_PATH( MPG123_INCLUDE_DIR2 
  NAMES mpg123.h.in 
  PATHS
  ~/Library/Frameworks
  /Library/Frameworks
  /usr/local
  /usr
  /sw # Fink
  /opt/local # DarwinPorts
  /opt/csw # Blastwave
  /opt
  extern/mpg123/src/libmpg123
)

IF( MPG123_INCLUDE_DIR2 )
  SET( MPG123_INCLUDE_DIRS ${MPG123_INCLUDE_DIRS} ${MPG123_INCLUDE_DIR2} )
ENDIF( MPG123_INCLUDE_DIR2 )

#MESSAGE( "MPG123_INCLUDE_DIR1: " ${MPG123_INCLUDE_DIR1} )
#MESSAGE( "MPG123_INCLUDE_DIR2: " ${MPG123_INCLUDE_DIR2} )
#MESSAGE( "MPG123_INCLUDE_DIRS: " ${MPG123_INCLUDE_DIRS} )

FIND_LIBRARY( MPG123_LIBRARIES 
  NAMES mpg123 libmpg123.lib
  HINTS
  PATH_SUFFIXES lib64 lib libs64 libs libs/Win32 libs/Win64 Release Debug
  PATHS
  ~/Library/Frameworks
  /Library/Frameworks
  /usr/local
  /usr
  /sw
  /opt/local
  /opt/csw
  /opt
  extern/mpg123/ports/MSVC++/2005
  extern/mpg123/ports/MSVC++/2008
  extern/mpg123/ports/MSVC++/2008clr
  extern/mpg123/ports/MSVC++/2010
)

SET( MPG123_FOUND 0 )
IF( MPG123_LIBRARIES AND MPG123_INCLUDE_DIRS )
  SET( MPG123_FOUND 1 )
  MESSAGE( STATUS "mpg123 found!" )
ELSE( MPG123_LIBRARIES AND MPG123_INCLUDE_DIRS )
  MESSAGE( STATUS "mpg123 not found..." )
ENDIF( MPG123_LIBRARIES AND MPG123_INCLUDE_DIRS )

FIND_PATH(LIBAO_INCLUDE_DIR ao.h /usr/include/ao /usr/local/include/ao)

FIND_LIBRARY(LIBAO_LIBRARIES NAMES ao PATH /usr/lib /usr/local/lib) 

IF (LIBAO_INCLUDE_DIR AND LIBAO_LIBRARIES)
  SET(LIBAO_FOUND TRUE)
ENDIF (LIBAO_INCLUDE_DIR AND LIBAO_LIBRARIES)

IF (LIBAO_FOUND)
   IF (NOT LIBAO_FIND_QUIETLY)
      MESSAGE(STATUS "Found libao: ${LIBAO_LIBRARIES}")
   ENDIF (NOT LIBAO_FIND_QUIETLY)
ELSE (LIBAO_FOUND)
   IF (LIBAO_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find libao")
   ENDIF (LIBAO_FIND_REQUIRED)
ENDIF (LIBAO_FOUND)

include_directories(${LIBAO_INCLUDE_DIR} ${MPG123_INCLUDE_DIRS})

set(ASSETDIR "${emotime_SOURCE_DIR}/assets" )
set(HEADERDIR "${emotime_SOURCE_DIR}/include" )
set(SRCDIR "${emotime_SOURCE_DIR}/src" )

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")

add_subdirectory(src

1 个答案:

答案 0 :(得分:0)

在CMake中,您可以使用以下命令实现与这些库的链接(不要将库添加到CXX_FLAGS!):

include_directories(${LIBAO_INCLUDE_DIR} ${MPG123_INCLUDE_DIRS})
target_link_libraries(target_name ${LIBAO_LIBRARIES} ${MPG123_LIBRARIES})

target_name需要替换为您的实际目标名称。