title: gcc compiles libavformat code, but g++ does not
body: <p>In order to determine the video duration for a given file, I use libavformat. My program looks as follows:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;libavformat/avformat.h&gt;
  #include &lt;libavutil/dict.h&gt;
  int main (int argc, char **argv) {
      AVFormatContext *fmt_ctx = NULL;
      int ret;
      if (argc != 2) {
          printf("usage: %s &lt;input_file&gt;\n", argv[0]);
          return 1;
      }
      av_register_all();
      if ((ret = avformat_open_input(&amp;fmt_ctx, argv[1], NULL, NULL)))
          return ret;
      int64_t duration = fmt_ctx-&gt;duration;
      int hours, mins, secs;
      secs  = duration / AV_TIME_BASE;
      mins  = secs / 60;
      secs %= 60;
      hours = mins / 60;
      mins %= 60;
      printf("Duration: %02d:%02d:%02d\n", hours, mins, secs);
      avformat_free_context(fmt_ctx);
      return 0;
  }
  </code></pre>
  
  <p>My problem is that while gcc compiles the code just fine, g++ does so without complaint as well but the created object file can neither be linked by gcc nor g++. Or to be more precise:</p>
  
  <pre><code>gcc -c duration.c
  gcc -o duration duration.o -lavformat
  ./duration my_movie.mp4
  </code></pre>
  
  <p>works. But this</p>
  
  <pre class="lang-bash prettyprint-override"><code>g++ -c duration.c # "works" as in "g++ does not complain"
  g++ -o duration duration.o -lavformat # (gcc produces the same output after compiling with g++)
  duration.o: In function `main':
  duration.c:(.text+0x41): undefined reference to `av_register_all()'
  duration.c:(.text+0x62): undefined reference to `avformat_open_input(AVFormatContext**, char const*, AVInputFormat*, AVDictionary**)'
  duration.c:(.text+0x18c): undefined reference to `avformat_free_context(AVFormatContext*)'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>does not work. This leads me to the conclusion that g++  does not produce code that can be linked properly (in this instance).</p>
  
  <p>I would really like to make this work with g++ because it is part of a bigger c++ project and it would be a bit of a mess always having to compile files that make use of this library with gcc instead. Does anyone know why g++ won't compile this program correctly?</p>