2012-04-02 22 views
0

mpg123ライブラリの機能をテストしているだけで、以下のコードを使用しています。私は、Linuxでのコマンドで次のように使用しています上記のコードをコンパイルするに'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); 
     } 
    //} 
} 

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

コンパイラはエラーを投げます。..リンカになっていません。 –

+0

https://github.com/LaurentGomila/SFML/wiki/SourceMp3Player –

+0

@ 0A0Dコンパイラがエラーを投げている理由を知っていますか?私には全く意味がありません。 – ShreyasD

答えて

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`