By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,486 Members | 1,696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,486 IT Pros & Developers. It's quick & easy.

how to define weak alias in user_defined library

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.