4

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.

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Gerhard
  • 41
  • 3
  • Please don't use what the hell kind of statements. Did you check with g++ compiler version? I mean downgrading it? I feel g++ is flaky sometimes. – Ankit Tanna Jul 15 '16 at 23:11
  • Sorry - I will not do it again. - g++ was OK, it was a missing extern-statement.Gerhard. – Gerhard Jul 16 '16 at 16:37

1 Answers1

3

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.

J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
  • Thanks a lot! That solved my problem. And please excuse, that this problem has been already solved somewhere else. I searched using the wrong key words. Have a good time! Gerhard. – Gerhard Jul 16 '16 at 16:33