Search code examples
cc-preprocessorextern

How to detect that extern "C" is in effect


I am trying to find all places where #include is placed inside extern C block. Is is possible to check this using preprocessor? I would like to do add something like this to my header files:

#ifdef EXTERN_C_IS_IN_EFFECT
#error File included from extern "C" block!
#endif

I am also looking for other ways to fail compilation in such case, e.g. use some special attribute. I am using gcc 4.4.7.

I defined following macro and then use it in every header which needs protection against inclusion in extern C block:

#define ASSERT_NO_EXTERN_C void assert_no_extern_c(int); void assert_no_extern_c(double);

Solution

  • Maybe you can define 2 function prototypes with same name and different parameters. You will get warnings in case of extern "C" block. And it is allowed in C++.