468,457 Members | 1,557 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

how to define weak alias in user_defined library

I want to write a library that has a defined function
(special_subroutine in
this example) aliased different names (special_subroutine_1 and
special_subroutine_2 in this example). i.e., I need to call a function
with
different names. Can someone give some help?

I try the following way, but it did not work. Can someone give me a
suggestion?
#gcc -c mylibrary.c
#gcc -c test.c
#gcc -o test test.o mylibrary.o

-------------------------------------------------------
//file name: test.c
//date: 07/09/2006
#include "mylibrary.h"
int main(int argc, char** argv){
...
special_subroutine_1( );
....
special_subroutine_2( );

}
------------------------------------------------------
//file name: mylibrary.c
//date: 07/09/2006
void special_subroutine(void){
....//some computation

}
#ifdef weak_alias
weak_alias(special_subroutine_1, special_subroutine)
weak_alias(special_subroutine_1, special_subroutine)
#endif
--------------------------------------------------

I think MPI library does something like this. e.g it has something like
weak_alias MPI_SEND with PMPI_SEND. Maybe pthread library also does
something similiar.

Jul 12 '06 #1
1 7064
On Wed, 12 Jul 2006, se*********@hotmail.com wrote:
I want to write a library that has a defined function
(special_subroutine in
this example) aliased different names (special_subroutine_1 and
special_subroutine_2 in this example). i.e., I need to call a function
with
different names. Can someone give some help?

I try the following way, but it did not work. Can someone give me a
suggestion?
#gcc -c mylibrary.c
#gcc -c test.c
#gcc -o test test.o mylibrary.o

-------------------------------------------------------
//file name: test.c
//date: 07/09/2006
#include "mylibrary.h"
int main(int argc, char** argv){
...
special_subroutine_1( );
....
special_subroutine_2( );

}
------------------------------------------------------
//file name: mylibrary.c
//date: 07/09/2006
void special_subroutine(void){
....//some computation

}
#ifdef weak_alias
weak_alias(special_subroutine_1, special_subroutine)
weak_alias(special_subroutine_1, special_subroutine)
#endif
You can use #defines, function pointers, or gcc extensions:

(1) Using #defines:
/*
* mylibrary.h
*
* It is the user's responsibility to modify this file
* when ``strong'' definitions become available.
*/
#define special_subroutine_1 special_subroutine
#define special_subroutine_2 special_subroutine

(2) Using function pointers:
/* mylibrary.h */
extern void (*special_subroutine_1)(void);
extern void (*special_subroutine_2)(void);

/*
* mylibrary.c
*
* It is the user's responsibility to reassign the
* pointers when ``strong'' definitions become
* available.
*/
void (*special_subroutine_1)(void) = special_subroutine;
void (*special_subroutine_2)(void) = special_subroutine;

(3) Using gcc extensions (off-topic on comp.lang.c):
#pragma weak special_subroutine_1 = special_subroutine
#pragma weak special_subroutine_2 = special_subroutine

Tak-Shing
Jul 12 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

94 posts views Thread by Gabriel Zachmann | last post: by
5 posts views Thread by Santiago Ordax Solivellas | last post: by
7 posts views Thread by Lance | last post: by
9 posts views Thread by Neelesh Bodas | last post: by
5 posts views Thread by Jon Shemitz | last post: by
1 post views Thread by channagoud | last post: by
4 posts views Thread by axr0284 | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.