2

我正在尝试使用ffmpeg它来编译一个简单的介绍性程序,以检查mp3编解码器是否可用。虽然代码编译正常,但我在解决链接器错误方面遇到了困难。这是代码:

#include <stdio.h>
#include <math.h>
#include <libavcodec/avcodec.h>

float *generateSinusoid(unsigned int sampleRate, unsigned int nSecondsAudio) {

  unsigned int nsamples = (nSecondsAudio * sampleRate);
  float *arr;
  arr = (float*) malloc(sizeof(float) * nsamples);
  int i = 0;

  for(i = 0; i < nsamples; i++) {
    arr[i] = 20 * sin(2.f * (M_PI) * (330/sampleRate) * i);  /*frequency of 330H
z*/
  }

  return arr;

}

int main(int argc, char *argv[]) {

  avcodec_register_all();

  AVCodec *codec;

  unsigned int sampleRate = 22050;  /*assumed.*/
  unsigned int nSecondsAudio = 4;
  float *arr;
  arr = (float *) malloc(sizeof(float) * nSecondsAudio * sampleRate);

  /*Step 1. Generate sinusoid.*/
  arr = generateSinusoid(sampleRate, nSecondsAudio);

  /* Step 2. See if encoder exists.*/
  /*codec = avcodec_find_encoder(AV_CODEC_ID_MP3);*/

  if(!codec) {  /*codec = NULL.*/
    printf("MP3 codec not found!!!!");
  } else {
    printf("MP3 codec found!!!");
  }

   return 0;
}  

代码是这样编译和链接的:

encoding_mp3: encoding_mp3.o
        gcc encoding_mp3.o -o encoding_mp3 -L/cygdrive/c/Users/Desktop/webserver/cygnus/lib/w32api -L/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/lib -lm -luser32 -lpthread -lavcodec

encoding_mp3.o: encoding_mp3.c
        gcc -I/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/include -I/cygdrive/c/Users/Desktop/webserver/cygnus/usr/include -g -c encoding_mp3.c -o encoding_mp3.o

clean:
        rm encoding_mp3.o encoding_mp3  

链接给出以下错误:

gcc -I/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/include -I/cygdrive/c/Users/Desktop/webserver/cygnus/usr/include -g -c encoding_mp3.c -o encoding_mp3.o
gcc encoding_mp3.o -o encoding_mp3 -L/cygdrive/c/Users/Desktop/webserver/cygnus/lib/w32api -L/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/ffmpeg_dev/lib -lm -luser32 -lpthread -lavcodec
encoding_mp3.o: In function `main':
/cygdrive/c/Users/Desktop/webserver/cygnus/ffmpeg/work/encoding_mp3.c:31: undefined reference to `_avcodec_register_all'
collect2: ld returned 1 exit status
make: *** [encoding_mp3] Error 1  

关于这个问题,我已经完成了关于 SO 的大部分线程,这是我迄今为止尝试过的:
- 将库放在所有非选项参数的末尾
- 注释掉引用函数的代码。这似乎有效。删除所有函数调用后,undefined reference错误就会消失,尽管结构的存在AVCodec不会导致任何问题。

非常欢迎对此提供任何帮助。

4

0 回答 0