0
Ich teste gerade die Funktionalität der mpg123 Bibliothek und ich verwende den unten gezeigten Code.undefined Verweis auf '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);
}
//}
}
den obigen Code kompilieren ich in Befehl in Linux die folgenden bin mit:
g++ -o mpg123example mpg123example.cpp -lmpg123
Und ich die folgende Fehlermeldung erhalten:
In function `openfile(mpg123_handle_struct*, char*)':
mpg123example.cpp:(.text+0x196): undefined reference to `mpg123_open'
collect2: ld returned 1 exit status
Meine Verwirrung ist, wenn die Bibliothek Wan't richtig verlinkt Warum sollte es sich nicht über die anderen Funktionsaufrufe beschweren? Irgendwelche Vorschläge würden geschätzt werden.
Es ist nicht an den Linker bekommen .. der Compiler wirft den Fehler. –
https://github.com/LaurentGomila/SFML/wiki/SourceMp3Player –
@ 0A0D Wissen Sie, warum der Compiler den Fehler verursacht? Für mich ergibt das überhaupt keinen Sinn. – ShreyasD