链接到libav *和mingw的未定义引用

时间:2013-10-17 14:01:24

标签: linker ffmpeg mingw libav

概述

我正在尝试使用mingw在Windows上的ffmpeg src目录中构建一个示例文件,但是我收到大量未定义的对libav*函数的引用。

我已经看过this question和其他几个人,但他们都没有解决我的问题。具体来说,我正在构建一个C ++程序,我使用g++,所以整个extern "C"的东西不适用(事实上)我已经尝试过了,但它没有用。)

详情

我已下载Zeranoe64-bit dev package,并将其解压缩到C:\apps\pf\ffmpeg。现在我只想在decoding_encoding.c中构建doc/examples文件。我的CWD是C:\apps\pf\ffmpeg\doc\examples,我的命令看起来像这样(添加了行继续和注释):

#Compile
gcc -o decoding_encoding.obj -c -IC:\apps\pf\ffmpeg\include decoding_encoding.c

#Link
gcc -o decoding_encoding.exe decoding_encoding.obj \
    -LC:\apps\pf\ffmpeg\lib -lavformat -lavcodec -lavdevice \
    -lavfilter -lavutil -lswscale -lm

编译工作正常,但链接会吐出这些错误:

decoding_encoding.obj:decoding_encoding.c:(.text+0xca): undefined reference to `av_get_channel_layout_nb_channels'
decoding_encoding.obj:decoding_encoding.c:(.text+0x131): undefined reference to `avcodec_find_encoder'
decoding_encoding.obj:decoding_encoding.c:(.text+0x179): undefined reference to `avcodec_alloc_context3'
decoding_encoding.obj:decoding_encoding.c:(.text+0x1fa): undefined reference to `av_get_sample_fmt_name'
decoding_encoding.obj:decoding_encoding.c:(.text+0x26c): undefined reference to `av_get_channel_layout_nb_channels'
decoding_encoding.obj:decoding_encoding.c:(.text+0x28f): undefined reference to `avcodec_open2'
decoding_encoding.obj:decoding_encoding.c:(.text+0x313): undefined reference to `avcodec_alloc_frame'
decoding_encoding.obj:decoding_encoding.c:(.text+0x3c8): undefined reference to `av_samples_get_buffer_size'
decoding_encoding.obj:decoding_encoding.c:(.text+0x3d6): undefined reference to `av_malloc'
decoding_encoding.obj:decoding_encoding.c:(.text+0x44f): undefined reference to `avcodec_fill_audio_frame'
decoding_encoding.obj:decoding_encoding.c:(.text+0x4ce): undefined reference to `av_init_packet'
decoding_encoding.obj:decoding_encoding.c:(.text+0x5c2): undefined reference to `avcodec_encode_audio2'
decoding_encoding.obj:decoding_encoding.c:(.text+0x63a): undefined reference to `av_free_packet'
decoding_encoding.obj:decoding_encoding.c:(.text+0x680): undefined reference to `avcodec_encode_audio2'
decoding_encoding.obj:decoding_encoding.c:(.text+0x6f8): undefined reference to `av_free_packet'
decoding_encoding.obj:decoding_encoding.c:(.text+0x722): undefined reference to `av_freep'
decoding_encoding.obj:decoding_encoding.c:(.text+0x72d): undefined reference to `avcodec_free_frame'
decoding_encoding.obj:decoding_encoding.c:(.text+0x738): undefined reference to `avcodec_close'
decoding_encoding.obj:decoding_encoding.c:(.text+0x743): undefined reference to `av_free'
decoding_encoding.obj:decoding_encoding.c:(.text+0x77b): undefined reference to `av_init_packet'
decoding_encoding.obj:decoding_encoding.c:(.text+0x7a1): undefined reference to `avcodec_find_decoder'
decoding_encoding.obj:decoding_encoding.c:(.text+0x7e9): undefined reference to `avcodec_alloc_context3'
decoding_encoding.obj:decoding_encoding.c:(.text+0x840): undefined reference to `avcodec_open2'
decoding_encoding.obj:decoding_encoding.c:(.text+0x8e6): undefined reference to `av_free'
decoding_encoding.obj:decoding_encoding.c:(.text+0x947): undefined reference to `avcodec_alloc_frame'
decoding_encoding.obj:decoding_encoding.c:(.text+0x999): undefined reference to `avcodec_get_frame_defaults'
decoding_encoding.obj:decoding_encoding.c:(.text+0x9c2): undefined reference to `avcodec_decode_audio4'
decoding_encoding.obj:decoding_encoding.c:(.text+0xa44): undefined reference to `av_samples_get_buffer_size'
decoding_encoding.obj:decoding_encoding.c:(.text+0xb7d): undefined reference to `avcodec_close'
decoding_encoding.obj:decoding_encoding.c:(.text+0xb88): undefined reference to `av_free'
decoding_encoding.obj:decoding_encoding.c:(.text+0xb96): undefined reference to `avcodec_free_frame'
decoding_encoding.obj:decoding_encoding.c:(.text+0xbe4): undefined reference to `avcodec_find_encoder'
decoding_encoding.obj:decoding_encoding.c:(.text+0xc2c): undefined reference to `avcodec_alloc_context3'
decoding_encoding.obj:decoding_encoding.c:(.text+0xcee): undefined reference to `av_opt_set'
decoding_encoding.obj:decoding_encoding.c:(.text+0xd08): undefined reference to `avcodec_open2'
decoding_encoding.obj:decoding_encoding.c:(.text+0xd8c): undefined reference to `avcodec_alloc_frame'
decoding_encoding.obj:decoding_encoding.c:(.text+0xe2f): undefined reference to `av_image_alloc'
decoding_encoding.obj:decoding_encoding.c:(.text+0xe83): undefined reference to `av_init_packet'
decoding_encoding.obj:decoding_encoding.c:(.text+0xfd3): undefined reference to `avcodec_encode_video2'
decoding_encoding.obj:decoding_encoding.c:(.text+0x105f): undefined reference to `av_free_packet'
decoding_encoding.obj:decoding_encoding.c:(.text+0x10a9): undefined reference to `avcodec_encode_video2'
decoding_encoding.obj:decoding_encoding.c:(.text+0x1135): undefined reference to `av_free_packet'
decoding_encoding.obj:decoding_encoding.c:(.text+0x117e): undefined reference to `avcodec_close'
decoding_encoding.obj:decoding_encoding.c:(.text+0x1189): undefined reference to `av_free'
decoding_encoding.obj:decoding_encoding.c:(.text+0x1194): undefined reference to `av_freep'
decoding_encoding.obj:decoding_encoding.c:(.text+0x119f): undefined reference to `avcodec_free_frame'
decoding_encoding.obj:decoding_encoding.c:(.text+0x1272): undefined reference to `avcodec_decode_video2'
decoding_encoding.obj:decoding_encoding.c:(.text+0x13b1): undefined reference to `av_init_packet'
decoding_encoding.obj:decoding_encoding.c:(.text+0x13fa): undefined reference to `avcodec_find_decoder'
decoding_encoding.obj:decoding_encoding.c:(.text+0x1442): undefined reference to `avcodec_alloc_context3'
decoding_encoding.obj:decoding_encoding.c:(.text+0x14ba): undefined reference to `avcodec_open2'
decoding_encoding.obj:decoding_encoding.c:(.text+0x153e): undefined reference to `avcodec_alloc_frame'
decoding_encoding.obj:decoding_encoding.c:(.text+0x1674): undefined reference to `avcodec_close'
decoding_encoding.obj:decoding_encoding.c:(.text+0x167f): undefined reference to `av_free'
decoding_encoding.obj:decoding_encoding.c:(.text+0x168a): undefined reference to `avcodec_free_frame'
decoding_encoding.obj:decoding_encoding.c:(.text+0x16ab): undefined reference to `avcodec_register_all'
collect2.exe: error: ld returned 1 exit status

2 个答案:

答案 0 :(得分:3)

我已经可以在README中看到这个库是用MinGW-w64构建的,目标是64位。普通的旧MinGW不支持64位目标。因此,如果您使用的是MinGW,那么它当然无法链接到64位库,因此您会遇到这些错误。

要解决您的问题,只需安装MinGW-w64即可。我强烈建议您从 here 下载其发行版。没有安装噩梦:只需解压缩,将bin添加到PATH环境变量中就可以了。

最后,看看这个 post ,了解谁是谁可能对您有用。

答案 1 :(得分:1)

为了关闭这个循环,我对mingw-w64不满意,特别是因为它没有libscrnsave的有用实现。因此,我使用标准libav从源代码构建了mingw

构建libav非常简单,一旦我完全理解了这一点。它是这样的:

  1. http://www.mingw.org/下载并安装mingw安装程序。
  2. 使用mingw安装程序安装MSYS。
  3. http://yasm.tortall.net/Download.html
  4. 下载yasm
  5. 将下载的yasm可执行文件重命名为yasm.exe,并将其放在bin下的MSYS目录中。
  6. https://libav.org/download.html
  7. 下载libav来源
  8. 解压缩libav资源。
  9. 在libav源所在的位置打开MSYS和cd
  10. 在MSYS中,运行./configure --disable-pthreads --disable-w32threads --prefix=/c/apps/pf/libav(这些是对我有用的选项,--prefix选项指定运行make install时库的安装位置,我将它们置于C:\apps\pf\libav下1}})。
  11. 完成后,运行make,然后make install
  12. 一旦构建了库,我就可以使用标准mingw从正常的Windows命令行链接到它们。

    要与pkg-config一起使用,您需要将已安装位置下的lib/pkgconfig目录添加到PKG_CONFIG_PATH env变量中。要正确构建,您应该使用pkg-config <LIBS> --cflags --libs来获取传递给gcc的正确标志,其中<LIBS>是您需要构建的libav库列表。只包含所有的libav库并没有任何损害,但是订单很重要。以下顺序似乎对我有用:libavdevice libavformat libavcodec libavfilter libswscale libavresample libavutil

    最终,我使用的命令行看起来像这样(添加了行继续和注释):

    #Compile
    gcc -o decoding_encoding.obj -c -IC:\apps\pf\libav\include decoding_encoding.c
    
    #Link
    gcc -o decoding_encoding.exe decoding_encoding.obj -LC:\apps\pf\libav\lib \
        -lavdevice -lavfilter -lavformat -lavcodec -lswscale -lavresample \
        -lavicap32 -lws2_32 -lpsapi -ladvapi32 -lshell32 -lavutil -lm