Including C Headers in C ++

I just read the SO question , discussing scenarios in which part of the code is valid in both C and C ++, but will lead to different behavior in each language.

The question is: Could this be a problem when including C headers in C ++ code?

I know from this question that you should include C headers as follows:

extern "C" { #include <your_os_or_library_header_in_c.h> } 

But all I have found so far is that extern "C" ensures that the name is turned off.

I could not find any information about whether it evaluates all statements as C, so, for example, sizeof('a') or 10 //* comment */ 2 (which you can find in the built-in function) are parsed as C, not C ++. (Note that relying on behaviors like someone who writes heading C is obviously a bad idea, but I ask for it from a purely academic point of view, β€œWhat if?”.)

Does the C ++ standard mean that the inclusion of a code block in extern "C" means that all its instructions must be parsed as C?

+6
source share
2 answers

extern "C" is a C ++ construct that affects language binding in a C ++ program. It does not switch the language to C in any way. All source code inside the extern "C" declaration is still processed as C ++ and only C ++.

Note that extern "C" affects not only the name definition - it is possible (theoretically) that functions with language connections in C and C ++ can have different calling conventions, for example.

+7
source

extern "C" only changes the link, i.e. manipulation and possibly a calling agreement. Try compiling and running this program with both C and the C ++ compiler:

 #include <stdio.h> #ifdef __cplusplus extern "C" { #endif inline void putsizeofchar() { printf("%zd\n", sizeof(char)); } #ifdef __cplusplus } #endif int main() { putsizeofchar(); return 0; } 

Since the inclusion of a title is a text replacement in both languages, the absence of a title does not matter.

+3
source

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


All Articles