[Mesa-dev] `extern "C" { #include "foo.h" }` is evil

Jose Fonseca jfonseca at vmware.com
Thu Dec 11 13:32:42 PST 2014


Including system's headers inside extern "C" { ... } is not safe, as system headers may have C++ code in them, and C++ code inside extern "C"  { .. } doesn't work.

This is because putting code inside extern "C" won't make __plusplus define go away, that is, the headers being included thinks is free to use C++ as it sees fits.

Including non-system headers inside extern "C"  is not safe either, because non-system headers end up including system headers, hence fall in the above case too.

Conclusion, includes inside extern "C" is simply not portable.


I said this before, but I failed to keep a close watch on this.  And the unfortunate thing is that sometimes things "appear" to work when the above rule is not observed, but it's enough to switch to slightly different SDKs / standard libraries and build fails miserably...

Yes this happens with MSVC headers which often have C++ on some places, but it could happen with any other OS really.


Please help do the right thing here, which is add extern "C" to headers themselves that need to be included from C++ source files.  Thanks.


Jose


More information about the mesa-dev mailing list