c++ - FFMPEG 在 C++ 中对 'avcodoec_open2' 的 undefined reference

标签 c++ linux ffmpeg

将 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”之类的示例?

最佳答案

The relevant lines of my Make file is similar to running g++ as below:

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

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

更新:

If you put the code supplied above in a CPP file, then run g++ with the supplied options, it does not work. You will get the error "undefined reference to `avcodec_open2'".

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

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

The order of the libraries worked for FFMPEG version 0.8, why does it not work with the latest version?

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

关于c++ - FFMPEG 在 C++ 中对 'avcodoec_open2' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10116231/

相关文章:

python - 如何在pyfiglet python3中获取所有可用的字体

php ffmpeg exec & shell_exec 进程在几秒后停止

c++ - 使用 cin 时的内存损坏 >>

c++ - Windows编程: Why do we cast lParam to CREATESTRUCT to get application state?

linux - mq_send 是原子的吗?

linux - 如何在 Amazon Linux AMI 上安装特定版本的 Docker

xml - FFProbe XML XPath

c# - 使用 ffmpeg-autogen 从 C# 中的 ffmpeg 复制 C 元数据示例 - av_dict_get 失败

c++ - 命名管道上的 WriteFile 有时会返回 ERROR_NO_DATA

C++ 试图用 OpenGL 绘制大图像时内存不足