0

I'm on Ubuntu 20.04 trying to compile some code that uses libav. Take the following example script:

// main.c

#include <libavcodec/avcodec.h>

int main()
{
    avcodec_find_encoder((enum AVCodecID) 0);
    return 0;
}

If I build this with gcc test.c -lavcodec it builds just fine, but if I build it with g++ test.c -lavcodec I get:

/usr/bin/ld: /tmp/ccHxMTp1.o: in function `main':
test.c:(.text+0xe): undefined reference to `avcodec_find_encoder(AVCodecID)'
collect2: error: ld returned 1 exit status
Jack M
  • 4,769
  • 6
  • 43
  • 67

1 Answers1

1

I think you're 'suffering' from C++ name mangling. Try wrapping the #include line with an extern "C" {, } pair...

extern "C" {
#include <libavcodec/avcodec.h>
}

int main()
{
    avcodec_find_encoder((enum AVCodecID) 0);
    return 0;
}
G.M.
  • 12,232
  • 2
  • 15
  • 18