C program cannot find function which included in header file

▼魔方 西西 提交于 2019-12-31 04:40:14

问题


I made program like this.

  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 }

My header file located in

root@ubuntu:/home/juneyoungoh/getDuration# find / -name "avformat.h"
/root/ffmpeg/libavformat/avformat.h
/usr/local/include/libavformat/avformat.h

then I run with gcc getDuration.c , but I show message like below.

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

Frankly, I do not have any idea what makes this.

Thanks for your answers.

========================== edited #1 ===========================

when I "ls /usr/local/lib", I get this.

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

you can see libavformat.a in the very first of the last line.

so if I command like what you suggest, I get below.

/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'

It is too long, so I just post little part of that.

I think all link of libavformat has been broken, But I do not know

what can I do to fix that link.

I have installed that their official link said.

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


回答1:


You have a linking error; not a compilation error. You will need to specify the library file by using -l option. I think it should be:

gcc getDuration.c -lavformat

If it does not work, specify the location of libavformat as

gcc -L/usr/local/lib getDuration.c -lavformat



回答2:


I found what wrong with my code(exactly, not code, but environment).

To execute that code, I need extra library which named "libavformat-dev".

In my case, since I am Ubuntu 12.04 user, I commanded like below.

apt-get install libavformat-dev

such a shame on me :(



来源:https://stackoverflow.com/questions/16849011/c-program-cannot-find-function-which-included-in-header-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!