Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while build a demo about ffmpeg , it occurs : undefined reference to `av_register_all'

Tags:

ffmpeg

This problem has bothered me for days.

After I compile and install ffmpeg , I try to build a demo using it, but it always fails.

The demo is:

#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"

int main(int argc,char *argv[]) {
    av_register_all();
    return 1;
}

With gcc main.c -o main.o, an error occurs: undefined reference to 'av_register_all'

Building with: gcc main.c -o main.o -lm -ld -lz -lavcodec -lavformat -lavutil, another error occurs: /usr/bin/ld: cannot find -ld

How can I resolve this?

like image 343
Cody Avatar asked Sep 17 '12 08:09

Cody


3 Answers

Putting includes within extern "C" block may work.

extern "C"{
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libswscale/swscale.h>
}
like image 104
ruwan800 Avatar answered Oct 08 '22 21:10

ruwan800


As mentioned here the order of libraries matters so in your case the following hopefully should work:

gcc main.c -o main.o -lavformat -lavcodec -lavutil  -lz -lm -lpthread
like image 25
Vladimir Avatar answered Oct 08 '22 22:10

Vladimir


gcc filename.c -o outputfilename -lavformat -lavcodec -lavutil  -lz -lm -lpthread -I'/usr/local/include' -lswresample

The above command will help to compile properly.

like image 20
nidi Avatar answered Oct 08 '22 20:10

nidi