How to use C code in C++

SadSeven picture SadSeven · Jul 3, 2013 · Viewed 35.6k times · Source

Just a small question: Can C++ use C header files in a program?

This might be a weird question, basically I need to use the source code from other program (made in C language) in a C++ one. Is there any difference between both header files in general? Maybe if I change some libraries... I hope you can help me.

Answer

RichieHindle picture RichieHindle · Jul 3, 2013

Yes, you can include C headers in C++ code. It's normal to add this:

#ifdef __cplusplus
extern "C"
{
#endif

// C header here

#ifdef __cplusplus
}
#endif

so that the C++ compiler knows that function declarations etc. should be treated as C and not C++.