【问题标题】:errors when compiling c code with ffmpeg library使用 ffmpeg 库编译 c 代码时出错
【发布时间】:2016-12-15 11:18:54
【问题描述】:

我已经在我的新 ubuntu 16.04 操作系统中安装了 ffmpeg 库。当我试图编译我的 c 代码时,我得到了以下奇怪的错误:

/home/widerstand/ffmpeg_build/lib/libavutil.a(hwcontext_vaapi.o): In function `vaapi_device_free':
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:861: undefined reference to `XCloseDisplay'
/home/widerstand/ffmpeg_build/lib/libavutil.a(hwcontext_vaapi.o): In function `vaapi_device_create':
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:891: undefined reference to `XOpenDisplay'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:896: undefined reference to `vaGetDisplay'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:903: undefined reference to `XDisplayName'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:893: undefined reference to `XDisplayName'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:917: undefined reference to `vaGetDisplayDRM'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:891: undefined reference to `XOpenDisplay'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:893: undefined reference to `XDisplayName'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:898: undefined reference to `XDisplayName'
/home/widerstand/ffmpeg_build/lib/libavutil.a(hwcontext_vdpau.o): In function `vdpau_device_create':
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vdpau.c:431: undefined reference to `XOpenDisplay'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vdpau.c:437: undefined reference to `XDisplayString'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vdpau.c:439: undefined reference to `XDefaultScreen'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vdpau.c:439: undefined reference to `vdp_device_create_x11'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vdpau.c:433: undefined reference to `XDisplayName'
/home/widerstand/ffmpeg_build/lib/libavutil.a(hwcontext_vdpau.o): In function `vdpau_device_free':
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vdpau.c:410: undefined reference to `XCloseDisplay'
collect2: error: ld returned 1 exit status
Makefile:30: recipe for target 'video_analysis' failed
make: *** [video_analysis] Error 1

我已经使用以下配置多次重新安装了 ffmpeg 库:

PATH="$HOME/bin:$PATH" PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig" ./configure \
  --prefix="$HOME/ffmpeg_build" \
  --pkg-config-flags="--static" \
  --extra-cflags="-I$HOME/ffmpeg_build/include" \
  --extra-ldflags="-L$HOME/ffmpeg_build/lib" \
  --bindir="$HOME/bin" \
  --enable-gpl \
  --enable-libass \
  --enable-libfdk-aac \
  --enable-libfreetype \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libtheora \
  --enable-libvorbis \
  --enable-libvpx \
  --enable-libx264 \
  --enable-nonfree
PATH="$HOME/bin:$PATH" make

安装过程每次都成功。但是当我尝试编译自己的c代码时,总是遇到与上面提到的相同的错误。我对此一无所知... 这是我使用的makefile:

编辑:

FFMPEG_LIBS=    libavdevice                        \
                libavformat                        \
                libavfilter                        \
                libavcodec                         \
                libswresample                      \
                libswscale                         \
                libavutil                          \

TARGET = video_analysis
LIBS = -lX11 -lm -lvdpau -lva
CC = gcc
CFLAGS += -O2 -g -O0
CFLAGS := $(shell pkg-config --cflags $(FFMPEG_LIBS)) $(CFLAGS)
LDLIBS := $(shell pkg-config --libs $(FFMPEG_LIBS)) $(LDLIBS)

.PHONY: default all clean

default: $(TARGET)
all: default

OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)

%.o: %.c $(HEADERS)
    $(CC) $(CFLAGS) -c $< -o $@

.PRECIOUS: $(TARGET) $(OBJECTS)

$(TARGET): $(OBJECTS)
    $(CC) $(OBJECTS) $(LDLIBS) $(LIBS) -o $@

clean:
    -rm -f *.o
    -rm -f $(TARGET)

编辑:

在第 30 行将 $(LIBS) 和 $(LDLIBS) 交换为 $(LDLIBS) $(LIBS) 之后,看起来好多了,但编译器仍然报告以下错误:

/root/ffmpeg_build/lib/libavutil.a(hwcontext_vaapi.o): In function `vaapi_device_create':
/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:896: undefined reference to `vaGetDisplay'
/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:917: undefined reference to `vaGetDisplayDRM'

我不知道仍然缺少哪个库?看起来好像 libva 仍然不见了?... 我自己得到了答案……看看这里:errors of 'vaGetDisplay' and `vaGetDisplayDRM'

【问题讨论】:

  • 您似乎忘记将您的应用程序与一些需要的库链接起来。搜索这些函数以查看需要哪些库。
  • 大部分缺少的函数都在 libX11 中(缺少 -lX11),对于 vaGetDisplay 和 vaGetDisplayDRM 以及 vdp_device_create_X11 我不知道。尝试使用谷歌搜索或人工查看它们是在哪个库中定义的。
  • @BrunoLevy 我已经安装了 libX11,并将 -lX11 放在我的 Makefile 中。但是错误并没有消失。 ffmpeg库有没有可能找不到libX11?安装libX11时是否需要配置路径以便ffmpeg库可以识别?
  • 你能给我们看看你的makefile吗? -lX11 应该在之后 -lffmpeg
  • @BrunoLevy 我已经上传了上面的makefile...

标签: c ffmpeg


【解决方案1】:

您需要链接提供这些功能的库以及 FFmpeg。

您需要添加的链接器标志是(以及它们各自的错误):

-lvdpau - libvdpau-dev

/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vdpau.c:439: undefined reference to `vdp_device_create_x11'

-lva - libva-dev

/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:896: undefined reference to `vaGetDisplay'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:917: undefined reference to `vaGetDisplayDRM'

-lX11 - libx11-dev

/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:861: undefined reference to `XCloseDisplay'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vaapi.c:898: undefined reference to `XDisplayName'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vdpau.c:431: undefined reference to `XOpenDisplay'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vdpau.c:437: undefined reference to `XDisplayString'
/home/widerstand/ffmpeg_sources/ffmpeg/libavutil/hwcontext_vdpau.c:439: undefined reference to `XDefaultScreen'

这些标志需要添加到您的-lav* 链接器标志之后。


编辑:OP 添加了生成文件

您还可以对所有库使用 pkg-config。

FFMPEG_LIBS=    libavdevice                        \
                libavformat                        \
                libavfilter                        \
                libavcodec                         \
                libswresample                      \
                libswscale                         \
                libavutil

PKG_CFG_LIBS=   x11                                \
                vdpau                              \
                libva                              \
                $(FFMPEG_LIBS)

TARGET = video_analysis
LIBS = -lm
CC = gcc
CFLAGS += -O2 -g -O0
CFLAGS := $(shell pkg-config --cflags $(PKG_CFG_LIBS)) $(CFLAGS)
LDLIBS := $(shell pkg-config --libs $(PKG_CFG_LIBS)) $(LDLIBS)

.PHONY: default all clean

default: $(TARGET)
all: default

OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)

%.o: %.c $(HEADERS)
    $(CC) $(CFLAGS) -c $< -o $@

.PRECIOUS: $(TARGET) $(OBJECTS)

$(TARGET): $(OBJECTS)
    $(CC) $(OBJECTS) $(LIBS) $(LDLIBS) -o $@

clean:
    -rm -f *.o
    -rm -f $(TARGET)

【讨论】:

  • 尽管我添加了 -lvdpau 和 -lva 标志,但我的编译器到现在仍然报同样的错误。见上面的makefile...
  • 使用您建议的 makefile,我收到以下错误:在 pkg-config 搜索路径中找不到包 libx11。也许您应该将包含 `libx11.pc' 的目录添加到 PKG_CONFIG_PATH 环境变量 No package 'libx11' found
  • 其实我之前已经安装了最新版的libx11-dev……libvdpau和libva也是这样……
  • @JoshdeKock 您解决编译/链接错误的方法非常好。结果表明,GCC 上标志的“顺序”也很重要。我也在另一个帖子上发布了这个问题的答案。 stackoverflow.com/questions/38995044/…
猜你喜欢
  • 2013-05-28
  • 2023-03-06
  • 2012-08-29
  • 1970-01-01
  • 2011-01-22
  • 2023-03-09
  • 1970-01-01
  • 2013-02-03
  • 2015-01-05
相关资源
最近更新 更多