[英]undefined reference to 'mpg123_open'
我正在测试mpg123库的功能,并且正在使用下面显示的代码。
#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
我的困惑是,如果库无法正确链接,为什么不抱怨其他函数调用呢? 任何建议,将不胜感激。
@zalman
# Makefile for mpg123example
# NOTES :
# $@ = 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`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.