【问题标题】:FFMPEG Undefined Reference to 'avcodoec_open2' in C++FFMPEG 未定义对 C++ 中“avcodoec_open2”的引用
【发布时间】:2012-04-24 08:46:26
【问题描述】:

在将 FFMPEG 库从 0.8 更新到“ffmpeg 版本 git-2012-04-12-277f20c”后编译我的一个 C++ 程序时出现错误

我在制作程序时遇到的错误如下:

-------- begin --------
Linking: Analysing_Server
./source/Encoding_Thread.o: In function `CEncoding_Thread::do_work()':
/home/Analyser/source/Encoding_Thread.cpp:155: undefined reference to `avcodec_open2'
collect2: ld returned 1 exit status
make: *** [Analysing_Server] Error 1

我的 Make 文件的相关行类似于运行 g++ 如下:

g++ test2.cpp -lavformat -lavcodec -lavutil -D__STDC_CONSTANT_MACROS

引发错误的相关 CPP 代码的精简版本是:

#include <stdio.h>
#include <stdint.h> 

#define LOG_OUT_STREAM_BUFF_SIZE  200000


extern "C"  {
  /* The ffmpeg library is completely written in C, so we need to tell the C++ compiler that so it links correctly. */
  #include "stdint.h"
  #include "libavcodec/avcodec.h"
  #include "libavutil/mathematics.h"
  #include "libswscale/swscale.h"
  #include "libavfilter/avfilter.h"

  int avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options);
  int avcodec_encode_video2(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr);
}

uint8_t m_outbuf[2][LOG_OUT_STREAM_BUFF_SIZE];
unsigned int m_out_size[2];
unsigned int m_OutBuffer_ID[2];
unsigned int m_Buffer_ID; /* This is just a uniqueish stamp we give to each buffer so we can tell when they change.. */

AVCodecContext * m_CodecContex;
AVCodec * m_codec;
struct SwsContext *m_img_convert_ctx;

unsigned char* m_DataBuff;

int Output_Width, Output_Height;
int Output_Bitrate;


int main(void) {
  //New version of FFMPEG calls this in avcodec_register_all
  //avcodec_init();

  /* register all the codecs */
  avcodec_register_all();

  /* Initalise the encoder */
  m_codec = avcodec_find_encoder(CODEC_ID_MP2);

  if (!m_codec) {
    printf("Encoding codec not found\n");
  }

  /* init the pointers.. */
  m_CodecContex = NULL;

  /* Default values.. */
  Output_Width = 1600;
  Output_Height = 1200;
  Output_Bitrate = 600000;

  /* Create/setup the Codec details.. */
  //Changed to work with new FFMPEG
  m_CodecContex = avcodec_alloc_context3(m_codec);
  avcodec_get_context_defaults3(m_CodecContex, m_codec);

  /* put sample parameters */
  m_CodecContex->bit_rate = Output_Bitrate;
  /* resolution must be a multiple of two */

  m_CodecContex->width = Output_Width;
  m_CodecContex->height = Output_Height;
  /* frames per second */
  m_CodecContex->time_base= (AVRational){1,25};

  m_CodecContex->gop_size = 10; /* emit one intra frame every ten frames */
  m_CodecContex->max_b_frames=1;
  m_CodecContex->pix_fmt = PIX_FMT_YUV420P; /* must be YUV for encoding.. */


  AVDictionary * RetunedAVDic;

  /* open it */
  //Changed to work with new FFMPEG
  if (avcodec_open2(m_CodecContex, m_codec, &RetunedAVDic) < 0) {
      printf("could not open codec");
  }
}

不幸的是,FFMPEG 附带的“doc/examples/decoding_encoding.c”下的示例不再有效,因为它使用的所有功能现在都已贬值。我的代码基于示例代码,在 FFMPEG 0.8 上运行良好,但不能与最新版本的 FFMPEG 一起编译。我已将一些已弃用的函数更改为较新的版本,但仍然无法编译。

有人知道我为什么会收到这个错误吗?或者是否有人使用最新版本的 FFMPEG 提供指向“doc/examples/decoding_encoding.c”等示例的链接?

【问题讨论】:

  • 当我在系统上安装的libavcodec 上运行nm -D /usr/lib/libavcodec.so.52 | grep avcodec_open 时,我得到以下输出:00000000002e4a80 T avcodec_open -- 没有avcodec_open2。您确定该符号应该可用吗?它是否需要比您(和我)安装的 libavcodec 的新版本或旧版本?
  • 为什么要在代码中手动定义avcodec_open2()avcodec_encode_video2(),而不是让编译器使用avcodec.h 中的声明?
  • sarnold,我最初使用的是 avcodec_open,但它在 avcodec.h 中的 cmets 中明确表示要使用 avcodec_open2。当我尝试使用 avcodec_open 代码编译但无法打开编解码器时,它曾经在 FFMPEG 0.8 上工作。我今天刚刚安装了最新版本的 FFMPEG,我可能会尝试看看是否可以回滚到更高版本的 FFMPEG。 Remy,我在某处读到,在 C++ 中使用 C 库时,您可能必须手动定义函数。如果您删除这两行代码,则会发生相同的错误。
  • sarnold,你是对的,avcodec_open2 仅在 libavcodec 54 及更高版本中,我似乎有 52 版本。感谢您的帮助

标签: c++ linux ffmpeg


【解决方案1】:

我的 Make 文件的相关行类似于运行 g++ 如下:

g++ test2.cpp -lavformat -lavcodec -lavutil -D__STDC_CONSTANT_MACROS

  1. 在编程中,细节很重要。您的链接命令与上述命令不够相似,否则它会起作用。
  2. 您可能将库放在链接行的错误位置。源和库的顺序matters

更新:

如果您将上面提供的代码放在 CPP 文件中,然后使用提供的选项运行 g++,它将不起作用。您将收到错误“未定义对 `avcodec_open2' 的引用”。

不,我没有。我得到一个不同的错误(因为我根本没有安装 avcodec)。

如果示例命令对您来说已经失败,那么您应该提供 it 产生的错误,而不是来自其他命令的错误,这样我们就不必猜测那个 other 命令可能看起来像。

FFMPEG 版本 0.8 的库顺序,为什么它不适用于最新版本?

可能是因为你安装了最新的libavcodec54,但没有安装最新的libavcodec-dev

【讨论】:

  • 1.如果将上面提供的代码放在 CPP 文件中,然后使用提供的选项运行 g++,它就不起作用。您将收到错误“未定义对 `avcodec_open2' 的引用”。 2. FFMPEG 0.8 版本的库顺序,为什么在最新版本中不起作用?我相信这些库的顺序是正确的。
猜你喜欢
  • 2014-07-26
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 2012-10-18
  • 1970-01-01
相关资源
最近更新 更多