[Ffmpeg-devel] [PATCH] rtp.h warning

Dave Dodge dododge
Thu Mar 22 22:33:58 CET 2007


On Thu, Mar 22, 2007 at 04:27:27PM -0500, Rich Felker wrote:
> On Thu, Mar 22, 2007 at 04:24:32PM -0400, Glenn Maynard wrote:
> > It's a C library; the external headers should compile in C++ code.  This
> 
> If you put extern "C" { } around them, then yes.

>From recent discussions around here I'm not sure everyone even agrees
on that point.

> Otherwise, all bets are off. C is not a subset of C++; the same code
> means different things in C++ than what it means in C.

Bear in mind that 'extern "C" {}' only affects linkage (such as
disabling name mangling).  It still otherwise expects the stuff
between the braces to be C++ and have C++ semantics.  Easy example:

  lib.h
  --------------------
  #include <stdlib.h>
  static inline int * alloc_obj(void) { return malloc(sizeof (int)); }
  ...


  code.cpp
  --------------------
  extern "C" {
  #include "lib.h"
  }
  ...


lib.h is valid C, but it is not valid C++.  Putting it within an
extern "C" block will not stop the C++ compiler from erroring on the
implicit conversion.

At least in that case you get an error; it could be worse:


  lib.h
  --------------------
  static inline int foo(void) { return sizeof 'x'; }
  ...


  lib.c
  --------------------
  #include "lib.h"
  ...


  code.cpp
  --------------------
  extern "C" {
  #include "lib.h"
  }
  ...


Here lib.h is valid C and C++ (so no error messages), but the return
value of foo() is very likely to be different between C and C++, even
within the extern "C" block.  If there is anything in lib.c or lib.h
that assumes foo() will be consistent in both the library and the code
linking to it, then you can expect runtime breakage.

                                                  -Dave Dodge




More information about the ffmpeg-devel mailing list