链接程序和库指南

定义符号引用

以下示例说明如何定义三种符号引用。然后,使用这些引用提取归档成员。虽然可以通过对链接编辑指定多个 -u 选项来实现归档提取,但此示例还说明了如何将符号的最终范围缩减到局部


$ cat foo.c

foo()

{

        (void) printf("foo: called from lib.a\n");

}

$ cat bar.c

bar()

{

        (void) printf("bar: called from lib.a\n");

}

$ cat main.c

extern  void    foo(), bar();



main()

{

        foo();

        bar();

}

$ ar -rc lib.a foo.o bar.o main.o

$ cat mapfile

{

        local:

                foo;

                bar;

        global:

                main;

};

$ cc -o prog -M mapfile lib.a

$ prog

foo: called from lib.a

bar: called from lib.a

$ nm -x prog | egrep "main$|foo$|bar$"

[28]    |0x00010604|0x00000024|FUNC |LOCL |0x0  |7      |foo

[30]    |0x00010628|0x00000024|FUNC |LOCL |0x0  |7      |bar

[49]    |0x0001064c|0x00000024|FUNC |GLOB |0x0  |7      |main

缩减符号范围一节中更详细地说明了将符号范围从全局缩减为局部的重要性。