Directly in the CPP write #include "c.h" reference to the C header file will be connected when this error occurs: Cpp.obj:error lnk2001:unresolved external symbol "int __cdecl Add (int,int) "([email=?add@ @YAHHH @z]?add@ @YAHHH @z[/email]), the root cause is that C + + and C differ in the way that functions are named.
C to the function name: _add
C + + On the name of the function: =?add@ @YAHHH @z
Solution: Do not modify the original C header file on the basis of the CPP file inside the extern "C" {#include "c.h"}, extern "C" {} means to interpret the code in C, so that c.h inside the code and functions are interpreted as C form. or modify the original C header file:
#ifdef __cplusplus
extern "C"
{
#endif
/*all of your code in here*/
#ifdef __cplusplus
}
#endif
Example:
C1.h:
#ifndef C1_h
#define C1_h
int addInC1 ();
#endif
C2.h
#ifndef C2_h
#define C2_h
#ifdef __cplusplus
extern "C"
{
#endif
int addInC2 ();
#ifdef __cplusplus
}
#endif
#endif
Cpp.cpp:
#include "C1.h"
#include "C2.h"
int main ()
{
int t1=addinc1 ();
int T2=addinc2 ();
};
Error
1 >cpp.obj:error lnk2019:unresolved external symbol _ADDINC2 referenced in function _main
2>cpp.obj:error lnk2019:unresolved external symbol "int __cdecl addInC1 (void)" (? addinc1@ @YAHXZ) referenced in funct Ion _main
Error 1 Cannot find C function _addinc2, error 2 is unable to find C + + function (? addinc1@ @YAHXZ).
Resolve Error 2:
Modify #include "C1.h"
To
extern "C"
{
#include "C1.h"
}
Resolve Error 1: Add a c2.c file to implement the addInC2. Add a c1.c file to achieve addInC1.
Reference: http://www.cnblogs.com/kenkofox/archive/2009/11/05/1597053.html