Extern "C"
When using C functions in the C ++ environment, the compiler often fails to find the C function definition in the OBJ module, resulting in a link failure. How can this problem be solved?
Answer and analysis:
During compilation, C ++ will combine function names and parameters to generate an intermediate function name to solve the problem of function polymorphism. c ++ does not, therefore, the corresponding function cannot be found during the link. In this case, the C function needs to use the extern "C" to specify the link. This tells the compiler to keep my name, do not generate an intermediate function name for the link for me.
Testc. h
# Ifndef _ testc # DEFINE _ testc # ifdef _ cplusplusextern "C" {# endifvoid show (); # ifdef _ cplusplus} # endif/* _ testc */
Testc. c
# Include "testc. H" void show () {printf ("Haha ");}
In vs2008, right-click testc. C and choose Properties> pre-compilation header> no pre-compilation header.
Then you can use it in C ++.