简体   繁体   中英

Cannot connect mpg123 and ao in project with cmakelists

I am using the cmakelists.txt below, that exists in the top folder of my project, and I am trying to connect mpg123 and ao to my project. In one .cpp file of the source folder I added a code with mpg123 and ao to play a song. This code can be compiled with this line:

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

I also added this line: target_link_libraries(emotime ${LIBAO_LIBRARIES} ${MPG123_LIBRARIES}) in the cmakelists that exists in my source folder. When I am running make I get errors like "undefined reference to ao_initialize'" and "undefined reference to mpg123_init'" in this piece of code:

    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

)

In CMake you achieve linking to those libraries using the following commands (don't add the libraries to CXX_FLAGS !):

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

were target_name needs to be replaced by your actual target's name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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