Jack M
Jack M

Reputation: 6015

g++ can't find libavcodec, but gcc can

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

Upvotes: 0

Views: 102

Answers (1)

G.M.
G.M.

Reputation: 12899

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;
}

Upvotes: 1

Related Questions