How to use extern "C"?

The project uses objects of C and C++. That arrangement to work, you must encapsulate ads With functions in extern "C" {...}.

I usually do it in header module

// my_c_header.h
#ifdef __cplusplus
extern "C" {
#endif

void spam(void);

#ifdef __cplusplus
}
#endif


and safely include the header in C++ file.

However, in the project meet type design
// my_cpp_header.hpp

extern "C" {
#include "my_c_header.h"
}
...

In this case, the my_c_header.h it is not spelled out.

To me it looks unsafe, as my_c_header.h can be included somewhere previously without extern "C" around, then the inclusion above will be ineffective. Well, in General the widespread use of extern "C" {} around the #include looks heavy.

What do you think about this? How do you prevent C and C++ in their projects?
July 9th 19 at 11:27
2 answers
July 9th 19 at 11:29
Solution
However, in the project meet type design
That's right. These parts of the project is written in C and involve the use in C.

Why code in C to check the presence of predefined preprocessor constants from another language?
But if it will be used more from a dozen other languages, then you ten such inspections will put their code in C?
So, Yes. Logical. A possible example of using C compilers other than C++. - Delpha_Cummings commented on July 9th 19 at 11:32
for example from the D language: https://dlang.org/spec/interfaceToC.html - otho.Mraz23 commented on July 9th 19 at 11:35
: thank you. I think it is solved using the macro type C_LINKAGE_BEGIN C_LINKAGE_END, which can vary depending on the compiler.

However, Your message is valid. Especially if Ssny code was written a long time without thinking about what it will be used in other projects. Thank you! - Delpha_Cummings commented on July 9th 19 at 11:38
: do not think, as it is in other languages will be used. You in these files, write in C, not in other languages. - otho.Mraz23 commented on July 9th 19 at 11:41
July 9th 19 at 11:31
How many people writing code?
Very much. - Delpha_Cummings commented on July 9th 19 at 11:34
Use the include .hpp which include original file with extern or macro. You with all participants is going to coordinate? - otho.Mraz23 commented on July 9th 19 at 11:37
Yeah, thought about it, too. But the number of files will grow. There is also the danger that someone will start to add C++code in include tion.hpp and it will be difficult to track. - Delpha_Cummings commented on July 9th 19 at 11:40
Then a macro when a lot of files . Includepurec - otho.Mraz23 commented on July 9th 19 at 11:43

Find more questions by tags C++C