Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link to ffmpeg libraries using mingw c++ -> undefined reference [duplicate]

I read about a lot of very similar problems here, but none of them seems to solve my problem. I feel that I need only a very small step to succeed...

I have an existing c++ project and try to include some FFmpeg functions. When linking with MinGW on Windows OS against the FFmpeg libraries, I get always the error message "undefined reference to...".

I reduced my code totally to the following sample:

// START of file simple.cpp
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif

#include <math.h>

#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>

int main(int argc, char **argv){
    avcodec_register_all();
    return 0;
}
// END of file simple.cpp

I compile with:

g++ -o simple.o -c simple.cpp -std=gnu++11

which worked well. My link command is:

g++ -o simple.exe simple.o -lavcodec -std=gnu++11

and I get the error message:

simple.o:simple.cpp:(.text+0xc): undefined reference to avcodec_register_all()

So, at least the libraries are found ;-) I used the libraries directly from Zeranoe and I also managed to compile new ones from source. The error message is the same in both cases.

A step to find the solution might be the following test (compile with gcc instead of g++):

  1. I rename simple.cpp to simple.c

  2. I compile with:

    gcc -o simple.o -c simple.c

  3. I link with:

    g++ -o simple.o -c simple.c -lavcodec

And that will work!!!

But, unfortunately, that is no solution for me, because my main program is definitely c++ und must be compiled using g++.

So, what the hell is wrong here?
Is that a FFmpeg topic or is that a gcc/g++ topic?
Please, all hints are welcome.

I already tried out all possible 'extern' and extern "C"- statements, no success.

like image 337
Gerhard Avatar asked Oct 27 '25 08:10

Gerhard


1 Answers

Many libraries have the following definitions in their header files

#ifdef __cplusplus
extern "C" {
#endif
...
#ifdef __cplusplus
}
#endif

This one does not do that. The following seems to work:

extern "C" {
#include <libavutil/opt.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>
}

Command nm simple.o will show the difference in symbol names.

like image 106
J.J. Hakala Avatar answered Oct 30 '25 00:10

J.J. Hakala