如何在automake中链接libavcodec、libavformat?

3

我正在一个C++项目中使用来自ffmpeg的libavcodec和libavformat库。使用g++编译器链接-lavcodec -lavformat可以正常工作,但是当我尝试在一个automake项目中编译相同的代码时,不知道出了什么问题。

正常运行:

g++ -o test -D__STDC_CONSTANT_MACROS -lavcodec -lavformat test.cpp

无法工作的 Makefile.am:
binaryname_LDFLAGS= -lavcodec -lavformat

错误:

....
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libavformat.a(nut.o):function 
ff_nut_add_sp: error: undefined reference to 'av_tree_node_size'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libavformat.a(nut.o):function 
ff_nut_add_sp: error: undefined reference to 'av_tree_insert'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libavformat.a(nut.o):function 
ff_nut_free_sp: error: undefined reference to 'av_tree_enumerate'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libavformat.a(nut.o):function 
ff_nut_free_sp: error: undefined reference to 'av_tree_destroy'
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/libavformat.a(rtp.o):function 
ff_rtp_get_payload_type: error: undefined reference to 'av_opt_get_int'
...

同样无法工作:

LDFLAGS=...-lavcodec -lavformat

错误:

src/dsp/audioDecoder.cpp:99: error: undefined reference to 'av_register_all'
src/dsp/audioDecoder.cpp:101: error: undefined reference to 'avcodec_find_decoder'
src/dsp/audioDecoder.cpp:109: error: undefined reference to 'avcodec_alloc_context'
src/dsp/audioDecoder.cpp:120: error: undefined reference to 'avcodec_open2'
src/dsp/audioDecoder.cpp:125: error: undefined reference to 'av_init_packet'
src/dsp/audioDecoder.cpp:188: error: undefined reference to 'avcodec_decode_audio3'

在第二种情况下,如果我不设置任何连接器,则无法找到包含头文件,因此链接器似乎被某种方式识别。
make V=1 返回:
make  all-am
make[1]: Go to '/path/to/trunk'
/bin/bash ./libtool --tag=CXX   --mode=link g++  -O2 -lrt -D__STDC_CONSTANT_MACROS   -o binaryname progsrc/binaryname/binaryname-binaryname.o    -lm  -lpthread -ldl -lmygeneratedlibrary
libtool: link: g++ -O2 -D__STDC_CONSTANT_MACROS -o binaryname progsrc/binaryname/binaryname-binaryname.o  -lm /path/to//trunk/.libs/lmygeneratedlibrary.a -lrt -lavutil -lavcodec -lavformat -lpthread -ldl
make[1]: Leave '/path/to/trunk'

我在这里做错了什么?
1个回答

5
CPPFLAGS 是用于 C 语言的预处理器,而不是 C++ 标志。对应的 C++ 标志是 CXXFLAGS。如果需要链接相关的内容,你需要使用 LDFLAGS
你的 Makefile.am 看起来不符合通常的 Makefile.am 格式。你可能需要更改为以下格式(参考自 这里):
# what flags you want to pass to the C compiler & linker
CFLAGS = # C compiler flags
LDFLAGS = # Linker flags

# this lists the binaries to produce, the (non-PHONY, binary) targets in
# the previous manual Makefile
bin_PROGRAMS = targetbinary1 targetbinary2 [...] targetbinaryN
targetbinary1_SOURCES = targetbinary1.c myheader.h [...]
targetbinary2_SOURCES = targetbinary2.c
.
.
targetbinaryN_SOURCES = targetbinaryN.c

此外,在包含FFmpeg的头文件时,请使用extern "C"将其括起来。例如:

extern "C" {
#include <libavformat/avformat.h>
}

好的,我首先构建了一个静态库(其中包括 libavcodec 的源代码),然后将其链接到一个二进制文件中。根据您的回答,我现在在 Makefile.am 中设置了 libraryname_LDFLAGS 和 binaryname_LDFLAGS 为 -lavformat -lavcodec,但仍然出现“未定义的引用”错误。我在这里漏掉了什么? - user2212461
1
@user2212461:-lavutil完整的 make V=1 输出如下: - Cornstalks
将-lavutil添加到链接器标志中并没有消除错误通知。我在我的帖子中添加了make V=1的输出。 - user2212461
如我在问题开头所述,-lavutil在我使用automake之前是不必要的。 - user2212461
@user2212461:-lavutil是必需的。libavformat依赖于它。如果您没有先遇到其他错误,那么您会收到有关其丢失的错误信息。 - Cornstalks
@user2212461:当你包含FFmpeg头文件时,是否用extern "C"{...}将它们包围起来了? - Cornstalks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接