用c文件调用c++的so库,一开始百度后,将so库源码中希望暴露出来的接口前加上extern “C”,以及条件编译,头文件中形如:
#ifndef __cplusplus
extern "C"
{
#endif
func_1;
func_2;
#ifndef __cplusplus
}
#endif
并将该头文件添加到测试工程,然后在测试工程里调用so库,编译时报错:expected identifier or '(' before string constant。
解决方案:
1. 将库源代码中的头文件改为:
extern "C"
{
func_1;
func_2;
}
2. 将测试工程中对应的头文件改为:
#ifdef __cplusplus
extern "C"
{
#endif
func_1;
func_2;
#ifdef __cplusplus
}
#endif
3. 添加c文件,调用该头文件中的函数,编译通过