Is extern “C” only required on the function declaration?

后端 未结 6 1916
执念已碎
执念已碎 2020-12-01 07:48

I wrote a C++ function that I need to call from a C program. To make it callable from C, I specified extern \"C\" on the function declaration.

6条回答
  •  不思量自难忘°
    2020-12-01 08:04

    It should be around both. The compiler needs to know to use the C symbol name and calling conventions when compiling the call sites (which may only see a declaration), and the compiler also needs to know to generate the C symbol name and use the C calling conventions when compiling the function definition itself (which may not see any other declarations).

    Now, if you have an extern-C declaration that is visible from the translation unit in which the definition exists, you may be able to get away with leaving off the extern-C from the definition, but I don't know that for sure.

提交回复
热议问题