__attribute__((weak)) zz

简介: http://blog.chinaunix.net/uid-7828352-id-4477460.html情况是这样的,碰到一个棘手的问题:我们不确定外部模块是否提供一个函数func,但是我们不得不用这个函数,即自己模块的代码必须用到func函数:extern int func(void);int a = func();if( a > .....){..........}我们不知道func函数是否被定义了这会导致2个结果:1:外部存在这个函数func,并且EXPORT_SYMBOL(func),那么在我自己的模块使用这个函数func,正确。

http://blog.chinaunix.net/uid-7828352-id-4477460.html

情况是这样的,碰到一个棘手的问题:我们不确定外部模块是否提供一个函数func,但是我们不得不用这个函数,即自己模块的代码必须用到func函数:

extern int func(void);

int a = func();

if( a > .....)

{

..........

}

我们不知道func函数是否被定义了

这会导致2个结果:

1:外部存在这个函数func,并且EXPORT_SYMBOL(func),那么在我自己的模块使用这个函数func,正确。

2:外部其实不存在这个函数,那么我们使用func,程序直接崩溃。

 

所以这个时候,__attribute__((weak)) 派上了用场。

在自己的模块中定义:

int  __attribute__((weak))  func(......)

{

return 0;

}

 

将本模块的func转成弱符号类型,如果遇到强符号类型(即外部模块定义了func),那么我们在本模块执行的func将会是外部模块定义的func。

如果外部模块没有定义,那么,将会调用这个弱符号,也就是在本地定义的func,直接返回了一个1(返回值视具体情况而定)

相当于增加了一个默认函数。

原理:连接器发现同时存在弱符号和强符号,有限选择强符号,如果发现不存在强符号,只存在弱符号,则选择弱符号。如果都不存在:静态链接,恭喜,编译时报错,动态链接:对不起,系统无法启动。

 

目录
相关文章
|
PyTorch TensorFlow 算法框架/工具
TensorflowConv2D:AttributeError: ‘int‘ object has no attribute ‘lower‘
TensorflowConv2D:AttributeError: ‘int‘ object has no attribute ‘lower‘
256 0
TensorflowConv2D:AttributeError: ‘int‘ object has no attribute ‘lower‘
undefined reference to symbol XGetWindowAttributes/cairo_destroy/XShapeGetRectangles
undefined reference to symbol XGetWindowAttributes/cairo_destroy/XShapeGetRectangles
131 0
解决办法:undefined reference to symbol 'pthread_mutexattr_settype@@GLIBC_2.2.5'
解决办法:undefined reference to symbol 'pthread_mutexattr_settype@@GLIBC_2.2.5'
278 0
When will the reference ATTRIBUTE_REF of an BOL entity be cleared
When will the reference ATTRIBUTE_REF of an BOL entity be cleared
111 0
When will the reference ATTRIBUTE_REF of an BOL entity be cleared
|
程序员 编译器 C语言
解决办法:undefined reference to symbol '_ZTVN10__cxxabiv117__class_type_infoE@@CXXABI_1.3
解决办法:undefined reference to symbol '_ZTVN10__cxxabiv117__class_type_infoE@@CXXABI_1.3
880 0
|
TensorFlow 算法框架/工具
has no attribute ‘swish‘
has no attribute ‘swish‘
153 0
|
C语言 编译器 C++