日期:2014-05-16 浏览次数:20690 次
$ cat foo.c #include <stdio.h> #ifndef VERSION_2 void foo(int f) { printf("version 1 called\n"); } #else void foo_v1(int f) { printf("version 1 called\n"); } __asm__(".symver foo_v1,foo@VERSION_1"); void foo_v2(int f) { printf("version 2 called\n"); } /* i.e. foo_v2 is really foo@VERSION_2 * @@ means this is the default version */ __asm__(".symver foo_v2,foo@@VERSION_2"); #endif $ cat 1.ver VERSION_1 { global: foo; local: *; }; $ cat 2.ver VERSION_1 { local: *; }; VERSION_2 { foo; } VERSION_1; $ cat main.c #include <stdio.h> void foo(int); int main(void) { foo(100); return 0; } $ cat Makefile all: v1 v2 libfoo.so.1 : foo.c gcc -shared -fPIC -o libfoo.so.1 -Wl,--soname='libfoo.so.1' -Wl,--version-script=1.ver foo.c libfoo.so.2 : foo.c gcc -shared -fPIC -DVERSION_2 -o libfoo.so.2 -Wl,--soname='libfoo.so.2' -Wl,--version-script=2.ver foo.c v1: main.c libfoo.so.1 ln -sf libfoo.so.1 libfoo.so gcc -Wall -o v1 -lfoo -L. -Wl,-rpath=. main.c v2: main.c libfoo.so.2 ln -sf libfoo.so.2 libfoo.so gcc -Wall -o v2 -lfoo -L. -Wl,-rpath=. main.c .PHONY: clean clean: rm -f libfoo* v1 v2 $ ./v1 version 1 called $ ./v2 version 2 called