Shall external “C” enclose the declaration or definition of a C++ function?

前端 未结 5 1946
轮回少年
轮回少年 2020-12-04 01:27

I saw in a cpp file that external \"C\" {...} encloses the definitions of several functions.

From https://isocpp.org/wiki/faq/mixing-c-and-cpp, I guess

5条回答
  •  隐瞒了意图╮
    2020-12-04 01:57

    You should enclose both declarations and definitions. "C" and "C++" functions are exported with different names. To produce correct "C" external name in object file extern "C" is needed in cpp, otherwise function will be exported with C++ name mangling . You also need to enclose those extern "C" { and corresponding } into #ifdef __cplusplus and #endif in header file, which is going to be #included by a C project to avoid C compilation error

提交回复
热议问题