所以我有一个非常示例代码来尝试解码 FFMPEG 视频流。 我的问题是 avcodec 不想链接,为此我全新安装了 Ubuntu 13.04。我按照此处的指南从源代码构建了 ffmpeg:https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu
我只想编译我的文件。请注意,我的 ubuntu 没有任何 avcodec 的实现或头文件。我使用的命令行是:
gcc -I/home/USER/ffmpeg_build/include -L/home/USER/ffmpeg_build/lib -lavcodec -o test.exe 下载/auv/src/dronerosvideo/src/ar2.cpp
/tmp/ccKTprFq.o:在函数 `fetch_and_decode(int, int, bool)' 中:
ar2.cpp:(.text+0x36e): 对“avcodec_register_all”的未定义引用
ar2.cpp:(.text+0x378): 对“av_log_set_level”的未定义引用
ar2.cpp:(.text+0x382): 对“avcodec_find_decoder”的未定义引用
ar2.cpp:(.text+0x3b1): 对“avcodec_alloc_context3”的未定义引用
ar2.cpp:(.text+0x3d6): 对“avcodec_open2”的未定义引用
ar2.cpp:(.text+0x46d): 对“av_init_packet”的未定义引用
ar2.cpp:(.text+0x50a): 对“avcodec_decode_video2”的未定义引用
ar2.cpp:(.text+0x534): 对“av_free_packet”的未定义引用
/tmp/ccKTprFq.o:(.eh_frame+0x13):未定义的引用 `__gxx_personality_v0'
collect2:错误:ld 返回 1 退出状态
只是为了进行理智的测试,如果我删除 -L 参数编译器会说:
/usr/bin/ld: cannot find -lavcodec
这意味着链接器在/home/USER/ffmpeg_build/lib中找到库。另外,如果我们检查库的实现情况,它是否存在:
nm ffmpeg_build/lib/libavcodec.a | grep "register_all"
0000000000000000 T avcodec_register_all
也按照建议,因为它是 C++,所以我在库的包含部分有
exten "C"
。
此时我完全没有任何想法,到底为什么编译失败?
首先,它是 C++,因此您需要使用
g++
而不是 gcc
,以便链接 C++ 标准库。这应该摆脱undefined reference to '__gxx_personality_v0'
。
然后,库的链接顺序实际上很重要。 您需要在使用它的对象(或源或其他库)之后指定一个库。
把它们放在一起,这样的命令行就可以工作(在我的测试中):
g++ -o test.exe -I$HOME/ffmpeg/include test.cc -L$HOME/ffmpeg/lib -lavcodec
(实际上,根据 ffmpeg 的构建方式,您可能还需要其他库,例如 pthreads 或 libx264)如果您安装了 pkg-config,可能可以只要求它提供正确的 C 标志 (
-cflags
) 和库:
# Since you didn't install ffmpeg to a known location, tell pkg-config about that location.
export PKG_CONFIG_PATH=$HOME/ffmpeg/lib/pkgconfig
g++ -o test.exe $(pkg-config -cflags libavcodec) test.cc $(pkg-config -libs libavcodec)