How to detect extern "C" acting

I am trying to find all the places where #include is placed inside an extern C block. Is it possible to verify this with a preprocessor? I would like to 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 a case, for example. use a special attribute. I am using gcc 4.4.7.

I defined the following macro and then use it in every header that needs protection against being included in an extern C block:

 #define ASSERT_NO_EXTERN_C void assert_no_extern_c(int); void assert_no_extern_c(double); 
+6
source share
2 answers

Perhaps you can define 2 function prototypes with the same name and different parameters. You will receive warnings in case of the external block "C". And this is allowed in C ++.

+11
source

This is not possible because the preprocessor is running before any parsing is performed; those. the preprocessor does not even know what extern "C" , and the action cannot depend on the existence of such a directive.

However, binding specifications have a nest, so instead of making sure that the switch doesn't specify extern "C" , you can put your header in the extern "C++" specification to make sure it uses the C ++ link.

+6
source

Source: https://habr.com/ru/post/985332/


All Articles