os_api.c
#include <stdio.h>
void func_os_api(void)
{
printf("I am OS API\n");
}
os_api.h
#ifndef __OS_API_H__
#define __OS_API_H__
extern void func_os_api(void);
#endif
call_os_api.c
#include <stdio.h>
#include "os_api.h"
void call_os_api(void)
{
printf("call_os_api start\n");
func_os_api();
printf("call_os_api end\n");
}
call_os_api.h
#ifndef __CALL_OS_API_H__
#define __CALL_OS_API_H__
extern void call_os_api(void);
#endif
native_os_api.c
#include <stdio.h>
void __attribute__((visibility("hidden")))func_os_api(void)
{
printf("I am native os API\n");
}
app.c
#include <stdio.h>
#include "os_api.h"
#include "call_os_api.h"
int main(int argc,char** argv)
{
func_os_api();
call_os_api();
return 0;
}
Makefile
CC = gcc
all: libosapi.so libcallosapi.so app
libosapi.so: os_api.c
$(CC) -o $@ $^ -I. -fPIC -shared
libcallosapi.so: call_os_api.c libosapi.so
$(CC) -o $@ $< -I. -fPIC -shared -L. -l:libosapi.so
app: app.c native_os_api.c
$(CC) -o $@ $^ -I. -L. -l:libcallosapi.so -l:libosapi.so
%.o: %.c
$(CC) -I. -c $^
clean:
rm *.so
rm app
>make
>export LD_LIBRARY_PATH=/home/chunxie/test_symb_hidden/
>./app
We can see two func_os_api() calls to different implementations.
We can use this feature when we need implement an OS API but avoid override OS API when some one really want it.