gcc - C程序找不到包含在头文件中的函数

标签 gcc ffmpeg header-files undefined-reference

我做了这样的程序。

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include "libavformat/avformat.h"
  5 
  6 int main (int argc, char* argv[]){
  7         av_register_all();
  8         return 0;
  9 }

我的头文件位于
root@ubuntu:/home/juneyoungoh/getDuration# find / -name "avformat.h"
/root/ffmpeg/libavformat/avformat.h
/usr/local/include/libavformat/avformat.h

然后我运行 gcc getDuration.c ,但我显示如下消息。
root@ubuntu:/home/juneyoungoh/getDuration# gcc getDuration.c 
/tmp/ccwjonqH.o: In function `main':
getDuration.c:(.text+0x10): undefined reference to `av_register_all'
collect2: ld returned 1 exit status

坦率地说,我不知道是什么原因造成的。

感谢您的回答。

========================== 编辑#1 ===================== ======

当我“ls/usr/local/lib”时,我明白了。
root@ubuntu:/home/juneyoungoh/getDuration# ls /usr/local/lib/
libavcodec.a   libavutil.a    libopus.la       libvpx.a   python2.7
libavdevice.a  libfdk-aac.a   libpostproc.a    libx264.a
libavfilter.a  libfdk-aac.la  libswresample.a  libyasm.a
libavformat.a  libopus.a      libswscale.a     pkgconfig

您可以在最后一行的第一行看到 libavformat.a。

因此,如果我按照您的建议进行命令,我会在下面。
/root/ffmpeg/libavformat/vqf.c:244: undefined reference to `av_free_packet'
/usr/local/lib//libavformat.a(vqf.o): In function `add_metadata':
/root/ffmpeg/libavformat/vqf.c:58: undefined reference to `av_malloc'
/root/ffmpeg/libavformat/vqf.c:64: undefined reference to `av_dict_set'
/usr/local/lib//libavformat.a(vqf.o): In function `vqf_read_header':
/root/ffmpeg/libavformat/vqf.c:148: undefined reference to `av_dict_set'
/root/ffmpeg/libavformat/vqf.c:208: undefined reference to `av_log'
/root/ffmpeg/libavformat/vqf.c:216: undefined reference to `av_malloc'
/root/ffmpeg/libavformat/vqf.c:170: undefined reference to `av_log'
/root/ffmpeg/libavformat/vqf.c:121: undefined reference to `av_log'
/root/ffmpeg/libavformat/vqf.c:184: undefined reference to `av_log'
/root/ffmpeg/libavformat/vqf.c:136: undefined reference to `av_log'
/usr/local/lib//libavformat.a(wavenc.o): In function `wav_write_trailer':
/root/ffmpeg/libavformat/wavenc.c:210: undefined reference to `av_rescale'
/usr/local/lib//libavformat.a(wavenc.o): In function `wav_write_packet':
/root/ffmpeg/libavformat/wavenc.c:181: undefined reference to `av_log'

太长了,我只发一小部分。

我认为 libavformat 的所有链接都已损坏,但我不知道

我该怎么做才能修复该链接。

我已经安装了他们的官方链接所说的。

https://ffmpeg.org/trac/ffmpeg/wiki/UbuntuCompilationGuideQuantal

最佳答案

你有一个链接错误;不是编译错误。您需要使用 -l 选项指定库文件。我认为应该是:

gcc getDuration.c -lavformat

如果它不起作用,请将 libavformat 的位置指定为
gcc -L/usr/local/lib getDuration.c -lavformat

关于gcc - C程序找不到包含在头文件中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16849011/

相关文章:

c -/usr/bin/ld : cannot find shared library

c++ - 是否可以使用 GCC 编译具有特定编译器标志的代码文件的一部分?

c - gcc-linux-gnueabi-arm 错误 undefined reference to `main'

android - 从 HLS 流中提取元数据(m3u8 文件)

ffmpeg - 使用 ffmpeg 合并视频

CodeBlocks header 公用文件夹

c - 带条件扩展的 GCC 宏?

video - FFMPEG 将 MP4 转换为其他视频格式而不损失质量

c++ - G++ 编译器错误 - 此处首先需要合成方法

C++ 方法定义和变量声明