1
#ifdef _cplusplus
   #include headerfile.h
#endif

I understand the concept behind this, though I don't exactly understand why it's needed.

If the purpose is to prevent "mangling" of headers when the code isn't compiled in C++:

-how is the code compiled if not in C++?

-how would the code continue to function without that header?

And tangentially:

-what circumstances would require this?

4 Answers 4

4
  • how is the code compiled if not in C++?

With C.

  • how would the code continue to function without that header?

That's why the header is included. If it would compile and work without the header with a C++ compiler, the #ifdef would be pretty pointless.

  • what circumstances would require this?

They may just bring in stuff needed when compiling with a C++ compiler. Maybe a debug macro needs to use std::cerr, then that header may include <iostream>. It could have millions of reasons.

Sign up to request clarification or add additional context in comments.

Comments

2

The usual reason for doing it is to give functions C or C++ linkage:

#ifdef _cplusplus
extern "C" {
#endif
#include headerfile.h
#ifdef _cplusplus
}
#endif

Comments

1

If C++ is not defined it would rely on non C++ specific header( possibly written in C)

What circumstance would require this is a good question.

1 Comment

That seems slightly obvious now that you've said it.
0

I know this thread is probably dead, but its still very much alive in search engines so I thought I would post some more detail. C++ mangles routine names (adds a bunch of characters) when they are compiled. I don't have the knowledge to tell you why it does that. I would guess to ensure that routines are uniquely identified among all the namespaces and classes in the program. Regardless, C does not mangle names. If you try to include a C header file in your project everything will work great during compilation, but when you link C++ will mangle the names of the routines in the C header file and try to find them in the object file for the C routines. It won't be able to find them because it expects them to be mangled.

This is most prevalent when you try to use a preexisting C library. In this case you can't recompile the C code as C++ to restore name mangling. So you have to tell the C++ compiler to look for C-style routines using 'extern "C"'. Putting it in the header file isn't necessary, but wherever a C++ compiler finds a declaration of a C routine it will need it. The header file placement is to avoid repeating yourself.

This can also happen in some IDEs. But basically results in a C library being linked with a C++ program/library. If gcc was used to compile it, g++ will have trouble figuring it out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.