55

I would like to compile this.

program.c

#include <libavcodec/avcodec.h>

int main(){
    int i = avpicture_get_size(AV_PIX_FMT_RGB24,300,300);
}

Running this

gcc -I$HOME/ffmpeg/include program.c

gives error

/tmp/ccxMLBme.o: In function `main':
program.c:(.text+0x18): undefined reference to `avpicture_get_size'
collect2: ld returned 1 exit status

However, avpicture_get_size is defined. Why is this happening?

alk
  • 66,653
  • 10
  • 83
  • 219
jamie_y
  • 1,201
  • 2
  • 11
  • 19

2 Answers2

77

However, avpicture_get_size is defined.

No, as the header (<libavcodec/avcodec.h>) just declares it.

The definition is in the library itself.

So you might like to add the linker option to link libavcodec when invoking gcc:

-lavcodec

Please also note that libraries need to be specified on the command line after the files needing them:

gcc -I$HOME/ffmpeg/include program.c -lavcodec

Not like this:

gcc -lavcodec -I$HOME/ffmpeg/include program.c

Referring to Wyzard's comment, the complete command might look like this:

gcc -I$HOME/ffmpeg/include program.c -L$HOME/ffmpeg/lib -lavcodec

For libraries not stored in the linkers standard location the option -L specifies an additional search path to lookup libraries specified using the -l option, that is libavcodec.x.y.z in this case.


For a detailed reference on GCC's linker option, please read here.

Neuron
  • 3,776
  • 3
  • 24
  • 44
alk
  • 66,653
  • 10
  • 83
  • 219
  • 1
    And probably something like `-L$HOME/ffmpeg/lib` in this case, since ffmpeg is in the user's home directory rather than installed to system directories. – Wyzard Mar 15 '14 at 16:42
  • Hi alk. Running 'gcc -I$HOME/ffmpeg/include program.c -lavcodes' gave me '/usr/bin/ld: cannot find -lavcodes collect2: ld returned 1 exit status' – jamie_y Mar 15 '14 at 17:00
  • 1
    Hi Wyzard. Running 'gcc -I$HOME/ffmpeg/include program.c -L$HOME/ffmpeg/lib' gave me the exact same error 'program.c:(.text+0x18): undefined reference to `avpicture_get_size' ' – jamie_y Mar 15 '14 at 17:02
  • 1
    Thanks! Running 'gcc -I$HOME/ffmpeg/include program.c -L$HOME/ffmpeg/lib -lavcodec' gives 6 undefined reference errors. Some of the methods are from libavcodec and the others are from libavutil. So I added -lavutil at the end of the command and it compiled. – jamie_y Mar 15 '14 at 17:54
  • 3
    This paragraph "Please also note that libraries need to be specified on the command line after the files needing them" saved my day, thanks. – kR105 Feb 20 '16 at 04:40
  • Shouldn't GCC be able to find libraries automatically by itself? Do we need to specify lib names when invoking make as well? – codezombie Nov 13 '19 at 05:22
  • For me, the hint about the order of the parameters was what solved my problem. Thanks a million! – xmjx Apr 18 '21 at 11:50
11

Are you mixing C and C++? One issue that can occur is that the declarations in the .h file for a .c file need to be surrounded by:

#if defined(__cplusplus)
  extern "C" {                 // Make sure we have C-declarations in C++ programs
#endif

and:

#if defined(__cplusplus)
  }
#endif

Note: if unable / unwilling to modify the .h file(s) in question, you can surround their inclusion with extern "C":

extern "C" {
#include <abc.h>
} //extern
Technophile
  • 425
  • 6
  • 9