小编Chr*_*ick的帖子

这是weakref的正确用法吗?

我想允许重新定义已在头文件中定义的.c文件中的函数.根据GCC手册中的weakref属性:

效果相当于将对别名的所有引用移动到单独的转换单元,将别名重命名为别名符号,将其声明为弱,编译两个单独的转换单元并对它们执行可重新加载的链接.

这听起来就像我想要做的那样.但是,以下示例不会编译错误:

tpp.c:18:13:错误:重新定义'foo'tpp.c:6:13:注意:'foo'的先前定义在这里

#include <sys/types.h>
#include <stdio.h>

/* this will be in a header file */
static void foo(void) __attribute__ ((weakref ("_foo")));

static void _foo(void)
{
    printf("default foo\n");
}

/* in a .c file #including the header mentioned above */
#define CUSTOM_FOO

#ifdef CUSTOM_FOO
static void foo(void)
{ 
    printf("user defined foo.\n");
}
#endif

int main(int argc, char **argv)
{
    printf("calling foo.\n");
    foo();
}
Run Code Online (Sandbox Code Playgroud)

我正确使用这个吗?我错过了什么?

gcc版本4.6.3(Ubuntu/Linaro 4.6.3-1ubuntu5)

c gcc

5
推荐指数
1
解决办法
1775
查看次数

标签 统计

c ×1

gcc ×1