What happens if I use extern "C ++" with the C toolchain?

My question is mainly related to the fact that the C ++ toolchain "understands" both C and C ++, so if I pass the code with extern "C" to the C ++ toolchain, I assume that it can understand what to do with it; but what if I feed the code using extern "C++" into the C toolchain?

What is the expected behavior?

+6
source share
4 answers

If the compiler ALSO understands C ++, it can accept it. If it is a pure C compiler, it will object (like extern "C" , since this syntax is not valid for C - that’s why it is usually enclosed in #ifdef __cplusplus or some)

+7
source

This is not supposed to compile, this is not a valid C syntax.

The standard approach to make C declarations in the header file work in both the C compiler and C ++ is to rely on the preprocessor character, which is defined only in the C ++ compiler. Like this:

 #ifdef __cplusplus extern "C" { #endif // C declarations here // ... #ifdef __cplusplus } #endif 

Each C ++ compiler defines __cplusplus.

+4
source

extern "C++" not valid C code, so the corresponding C compiler should issue a diagnostic. There is no requirement that he does not compile the code. Having released the diagnostics, the compiler is free to execute everything that was determined by its developer.

+4
source

If you wrote some other compiler with this option, you can do it.

As @Mats replied, we got another procedure for this: #ifdef __cplusplus . Moreover, what you are trying is just a mistake.

+2
source

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


All Articles