When you use g++, __cplusplus will be defined automatically. (you
can’t undef it in fact.) When you use gcc, __cplusplus is not defined.
At the same time, When you use g++ to compile a C file, although file
extension is .c, but g++ still use name mangling to change function
name. (The conclusion is based on g++ and gcc in Linux system)
If you have c and cpp source file together, you can just use g++ compile
them all. You don’t need any __cplusplus syntax. g++ compile all
files using name mangling. (look them as c++ files). At this time file
extension doesn’t play a role at all.
If you c++ file want to use a c function. You don’t have c function source
code(It is in a lib or obj file) or you don’t want to recompile it( it’s a very
big C library). At this time, you should use " extern "C" " in head file or
function declaration.
If you define a function in a.cpp file(You have to use g++ to compile it),
and this function will be used in legacy C system, you need the previous
trick again. give lib and head file to C system, and then the C system can
include head file and linked to lib.
In one word, if you have obj code produced by C or C++, Whenyou want to linked it to current different language, you shouldconsider using "__cplusplus"