extern AVCodec h264_decoder;
}
直接在Cpp里面写 #include "c.h"引用C头文件会在连接的时候出现此类错误:cpp.obj : error LNK2001: unresolved external symbol "int __cdecl add(int,int)" ([email=?add@@YAHHH@Z]?add@@YAHHH@Z[/email]), 根本原因在于C++ 和 C在对函数命名方式的不同。
C对函数的命名: _add
C++对函数的命名:
解决方案:
- 在不修改原来C头文件的基础之上 ,在cpp文件里面 extern "C" {#include "c.h"} ,extern "C" {} 表示用C的方式解释代码,这样c.h里面的代码及函数都被解释成C形式。
- 或者修改原来C头文件:
#ifdef __cplusplus
extern "C"
{
#endif
/*all of your code in here*/
#ifdef __cplusplus
}
#endif
例子:
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)" () referenced in function _main
错误1是找不到C函数 _addInC2 ,错误2是找不到C++函数 ().
解决错误2:
修改 #include "c1.h"
到
extern "C"
{
#include "c1.h"
}
解决错误1:加一个c2.c文件,去实现addInC2.加一个c1.c文件,去实现addInC1.
参考http://www.cnblogs.com/kenkofox/archive/2009/11/05/1597053.html