2012-04-02 75 views
0

我只是測試mpg123庫的功能,我使用下面顯示的代碼。未定義的引用'mpg123_open'

#include <stdlib.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <fcntl.h> 
#include <cstring> 
#include <mpg123.h> 

#define INBUFF 16384 
#define OUTBUFF 32768 

void openfile(mpg123_handle* mh , char* filename){ 
    int    fnum; 
    int    rbytes; 
    int    mpg123Status = 0; 
    int    channels = 0, encoding = 0; 
    long   rate = 0; 
    size_t   size; 
    int    decodedbytes, decodestatus; 
    int    fileSizeSamples; 
    unsigned int framesConsumed; 
    unsigned char *mp3InBuf, *mp3OutBuf; 
    unsigned int mp3InBufSize, mp3OutBufSize; 
    size_t   decodedNow; 

    mp3InBufSize = 2048; 
    mp3OutBufSize = 32768; 

    mp3InBuf  = new unsigned char[mp3InBufSize]; 
    mp3OutBuf  = new unsigned char[mp3InBufSize]; 

    //Open mp3 file for reading 
    fnum = open(filename, O_RDONLY); 
    if(fnum < 0){ 
     printf("ERROR opening file: %s\n", strerror(fnum)); 
     exit(0); 
    } 

    decodedbytes = 0; 
    decodestatus = MPG123_NEED_MORE; 

    mpg123Status = mpg123_init(); 
    if(mpg123Status){ 
     printf("Could not init MPG123: %d - %s\n", mpg123Status, mpg123_plain_strerror(mpg123Status)); 
     close(fnum); 
     exit(0); 
    } 

    mh = mpg123_new(NULL, &mpg123Status); 
    if(mh == NULL){ 
     printf("Could not open mpg123_handle: %d - %s\n", mpg123Status, mpg123_plain_strerror(mpg123Status)); 
     close(fnum); 
     exit(0); 
    } 

    mpg123Status = mpg123_open_feed(mh); 
    if (mpg123Status) { 
     printf("Could not open mpg123 feed: %d - %s\n", mpg123Status, mpg123_plain_strerror(mpg123Status)); 
     close(fnum); 
     exit(0); 
    } 

    mpg123_open(mh, filename); 
    fileSizeSamples = MPG123_ERR; 
    framesConsumed = 0; 

    /* determine file parameters */ 
    mpg123Status = -1; 
    printf("Start initial decode for file params.\n"); 
    while (MPG123_NEW_FORMAT!=mpg123Status) { 
     /* file -> decoder */ 
     rbytes = read(fnum, mp3InBuf, mp3InBufSize); 
     if (rbytes>0) { 
      mpg123Status = mpg123_decode(mh, mp3InBuf, rbytes, mp3OutBuf, mp3OutBufSize, &size); 
      if ((mpg123Status) && (MPG123_NEW_FORMAT!=mpg123Status)) { 
       printf("Could not feed mpg123: read %d Bytes %d from file %d - %s\n",size, rbytes, mpg123Status, mpg123_plain_strerror(mpg123Status)); 
       close(fnum); 
       exit(0); 
      } 
       mpg123_getformat(mh, &rate, &channels, &encoding); 
     } 
    } 

    printf("MP3 at %d Hz %d channels %x encoding\n", rate, channels, encoding); 

    /*Read entire file*/ 
    while(!EOF){ 
     rbytes = read(fnum,mp3InBuf,mp3InBufSize); 
     decodestatus = mpg123_decode( mh, mp3InBuf, mp3InBufSize, 
             mp3OutBuf,mp3OutBufSize, &decodedNow); 
     printf("%s",mp3OutBuf); 
    } 

    //Clean up 
    close(fnum); 
    mpg123_close(mh); 
    mpg123_delete(mh); 
    mpg123_exit(); 
    delete [] mp3InBuf; 
    delete [] mp3OutBuf; 
} 

int main(int argc, char **argv) { 
    mpg123_handle* mh; 

    //for(;;){ 
     for(int i=1; i<argc; i++){ 
      printf("Opening File: %s\n", argv[i]); 
      openfile(mh, argv[i]); 
      sleep(5); 
     } 
    //} 
} 

編譯上面的代碼,我使用的命令在Linux中的以下內容:

g++ -o mpg123example mpg123example.cpp -lmpg123 

而且我收到以下錯誤:

In function `openfile(mpg123_handle_struct*, char*)': 
mpg123example.cpp:(.text+0x196): undefined reference to `mpg123_open' 
collect2: ld returned 1 exit status 

我的困惑是,如果庫爲什麼它不會抱怨其他函數調用?任何建議,將不勝感激。

+0

它沒有得到該鏈接..編譯器引發錯誤。 – 2012-04-02 17:01:17

+0

https://github.com/LaurentGomila/SFML/wiki/SourceMp3Player – 2012-04-02 17:04:53

+0

@ 0A0D你知道爲什麼編譯器可能會拋出錯誤嗎?我根本沒有意義。 – ShreyasD 2012-04-02 17:24:15

回答

0

@zalman

# Makefile for mpg123example 

# NOTES : 
# [email protected] = target name 
# $< = implicit source 

PLATFORM=x86 

# We assume to always build on an X86, so no cross compile for it 

# We assume to always build on an X86, so no cross compile for it 

# ARM Build Specifics 
ifeq ($(PLATFORM), vs) 
    CROSSCOMPILE=arm-none-linux-gnueabi- 
    ROOTFS=/projects/armrfs/armel_dev02 
    PLATFORMSTR="VS Exciter Board" 
    CROSSLIBDIR=/projects/CodeSourcery/Sourcery_G++_Lite/arm-none-linux-gnueabi/libc 
endif 

# X86 Build Specifics 
ifeq ($(PLATFORM), x86) 
    CROSSCOMPILE= 
    ROOTFS= 
    PLATFORMSTR="Generic x86" 
    CROSSLIBDIR= 
endif 

CFLAGS=-I $(ROOTFS)/usr/include/ -DPLATFORM=$(PLATFORM) -D_FILE_OFFSET_BITS=64 
LDFLAGS=-L $(CROSSLIBDIR)/usr/lib/ -L $(ROOTFS)/usr/lib -L $(ROOTFS)/usr/local/lib -lmpg123 

CPP=$(CROSSCOMPILE)g++ 

CPPSRC= mpg123example.cpp 

all: mpg123example 

mpg123example: $(CPPSRC) 
    $(CPP) -o mpg123example $(CFLAGS) $(LDFLAGS) mpg123example.cpp 


clean: 
    rm -rf *.o mpg123example`