【问题标题】:Undefined reference to avcodec_alloc_context but ffmpeg linker order correct?对 avcodec_alloc_context 的未定义引用但 ffmpeg 链接器顺序正确?
【发布时间】:2014-09-13 02:50:38
【问题描述】:

我想构建一个静态链接的可执行文件,静态链接到 libavcodec 和 libavformat。 静态 ffmpeg 库是用以下方式构建的:

./configure --enable-static --enable-gpl --enable-nonfree --disable-vaapi 
     --disable-libopus --prefix=myBuild --disable-swresample

链接器设置如下:

g++ -O2 -static -o myBin myBin-myBin.o someotherlibraries.a 
     -L/ffmpeg/myBuild/lib -lavformat -lavcodec -lavutil  -lrt -lm -lpthread -lz

编译时,我收到 ONLY ONE 错误消息 >:-/

src/ffmpeg/myProgram.cpp:115: error: undefined reference to 'avcodec_alloc_context'

nm /ffmpeg/myBuild/lib/libavcodec.a 的输出 | grep avcodec_alloc_context :

         U avcodec_alloc_context3
         U avcodec_alloc_context3
000003c0 T avcodec_alloc_context3
         U avcodec_alloc_context3

我将 libavcodec.h 包含在 extern "C" {} 中,并且我相信我的静态链接器顺序是正确的。为什么我会收到此错误?是因为这种方法已被弃用吗?我该如何解决这个问题?

解决方案:

不要使用

avCtx = avcodec_alloc_context()

可能来自较旧的代码 sn-ps,但使用

codec = avcodec_find_decoder(CODEC_ID_XYZ);//for completeness but should be the same as before
avCtx = avcodec_alloc_context3(codec)

【问题讨论】:

  • nm /ffmpeg/myBuild/lib/libavcodec.a | grep avcodec_alloc_context 的输出是什么?
  • 我将输出添加到我的帖子中
  • @PaoloBonzini 这能告诉你什么吗?

标签: c++ ffmpeg static-linking libavcodec libavformat


【解决方案1】:

您是否尝试改为调用 avcodec_alloc_context3?

调用 avcodec_alloc_context3 没有问题,分配额外数据然后调用 avcodec_open2。

链接顺序也应该是-lavutil -lavformat -lavcodec

【讨论】:

  • 太棒了,成功了!!我不知道我从哪里得到这个旧代码 sn-p。他们是否在某个时候删除了已弃用的版本?
  • 很高兴知道它有效。也许他们删除了它,因为它们不能与较新的调用互换。
【解决方案2】:

如果我没记错的话,我们也遇到了问题,解决方案是您必须专门将 libavcodec.a(连同完整路径)和其他 ffmpeg 静态库添加到 g++ 链接步骤。看看它是否可以这样工作。

此外,库的顺序也很重要。我不再有旧的 makefiels,但也许记得 libavutil 应该是列表中的第一个。

所以你的链接命令应该是这样的:

g++ -O2 -static -o myBin myBin-myBin.o someotherlibraries.a 
 /ffmpeg/myBuild/lib/libavutil.a 
 /ffmpeg/myBuild/lib/libavformat.a 
 /ffmpeg/myBuild/lib/libavcodec.a 
  -lrt -lm -lpthread -lz

【讨论】:

  • 关于绝对库路径:您建议的方式和 -L 内部是否完全相同?无论如何都没有区别。我使用的顺序在我的情况下似乎是正确的。顺序不是表示依赖关系,它们是向后读取的吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 2014-08-20
  • 2016-09-19
  • 2015-05-25
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 2011-12-08
相关资源
最近更新 更多