using __attribute__ ((weak)) leads to warning "redundant redeclaration of xxx", any way to avoid?
When I use __attribute__ ((weak))
like in this post I get warnings from gcc about redeclaring the symbol, while all I do is adding an attribute. Can the attribute be attached differently? The warnings I get l开发者_StackOverflow社区ook like this:
threads.c:53: warning: redundant redeclaration of ‘pthread_once’
/usr/include/pthread.h:478: note: previous declaration of ‘pthread_once’ was here
Yes - GCC allows you to use #pragma weak
to declare symbols as weak, so you can do this instead:
#include <pthread.h>
#pragma weak pthread_create
#pragma weak pthread_mutex_init
#pragma weak pthread_mutex_lock
#pragma weak pthread_mutex_unlock
#pragma weak pthread_mutex_destroy
/* ... code ... */
(Documented here.)
You can use a pthread stub library like the one from http://cgit.freedesktop.org/xcb/pthread-stubs/ which avoids the need to create your own stubs.
If you only need to run on fairly modern systems, either libc will provide a set of stubs for most commonly used functions for making things thread-safe or libpthread is integrated into libc. Note that stubs for pthread_once
may not call the passed function ever. (Some libraries use this to detect if they are in the threaded or unthreaded programming environment.)
精彩评论