[cfe-dev] C++ modules imported in extern "C"

Seth Cantrell seth.cantrell at gmail.com
Wed Oct 1 19:54:57 PDT 2014


With C++ modules being far enough along to be included in -fmodules now I tried enabling -fmodules on a small C++ program I have laying around. I was impressed that it almost all worked right away.

Here's the issue I ran into with one header:


glcorearb.h:616:1: error: import of C++ module 'Darwin.C.stddef' appears within extern "C" language linkage specification
#include <stddef.h>
^
glcorearb.h:5:1: note: extern "C" language linkage specification begins here
extern "C" {
^
glcorearb.h:1517:1: error: import of C++ module 'Darwin.C.inttypes' appears within extern "C" language linkage specification
#include <inttypes.h>
^
glcorearb.h:5:1: note: extern "C" language linkage specification begins here
extern "C" {
^


Even without modules it seems like people should be taking care not #include a header in a context that may not be compatible with the content of those headers. Here we are building C++ and stddef.h and inttypes.h are therefore C++ headers that might not work in an extern "C" context. Platforms owners, who are in control of both the including file and the included file can legitimately do this, but I don't think this is good practice when included someone else's header. However the standards don't clearly disallow this and I have certainly seen circumstances where people recommended extern "C" { #include <system header> } for various reasons.

In this specific case it's not even deliberate as far as I can tell, but it's worked and so no one's bothered to fix it here. A quick hack to remove these #includes from extern "C" solved the issue for me.

Anyway, I checked http://clang.llvm.org/docs/Modules.html#modularizing-a-platform but didn't notice any recommendations that apply here. So what should be recommended? Is this an 'anti-pattern' for modules? An anti-pattern for 'well organized' non-module headers? Should modules be able to coexist with these kinds of shenanigans when the code worked fine pre-modules? Should I contact the developers of this header and recommend a change? (It's an automatically generated header from gl3w.)

Thanks,
Seth



More information about the cfe-dev mailing list