一、偶遇 error: undefined reference to xxx 问题
尝试封装通用的接口到一个private.so,然后供客户端使用,private.so编译出来后由sample.cpp依赖调用其中封装的接口,但是一直报error: undefined reference to xxx的错误,并且检查so、头文件都依赖正确,c方式编译的函数也用extern "C" 声明。
#ifdef __cplusplusextern "C"{#endifxxx
#ifdef __cplusplus
}#endif
于是用如下方法查看so的符号表根本找不到定义的 xxx 函数:
readelf -s private.so
nm-D private.so
从Android.mk定位问题发现编译参数 CAMX_CFLAGS += -fvisibility=hidden ,此参数的作用就是将函数名隐藏,需要暴露给用户的函数接口可以单独通过 __attribute__((visibility ("default"))) 声明避免被隐藏。
LOCAL_CFLAGS :=$(CAMX_CFLAGS)
LOCAL_CPPFLAGS := $(CAMX_CPPFLAGS)
因此如下声明后,xxx函数就能被链接找到,编译通过。
#ifdef __cplusplus
extern "C"{ #endif __attribute__((visibility ("default")))xxx #ifdef __cplusplus } #endif
-fvisibility 参数具体说明如下: