how to define weak alias in user_defined library

S

seyong_choi

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
 
T

Tak-Shing Chan

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,166
Latest member
DollyBff32
Top