Restricting symbols in a Linux static library

后端 未结 5 733
逝去的感伤
逝去的感伤 2020-12-02 11:40

I\'m looking for ways to restrict the number of C symbols exported to a Linux static library (archive). I\'d like to limit these to only those symbols that are part of the

5条回答
  •  旧巷少年郎
    2020-12-02 12:18

    Static libraries can not do what you want for code compiled with either GCC 3.x or 4.x.

    If you can use shared objects (libraries), the GNU linker does what you need with a feature called a version script. This is usually used to provide version-specific entry points, but the degenerate case just distinguishes between public and private symbols without any versioning. A version script is specified with the --version-script= command line option to ld.

    The contents of a version script that makes the entry points foo and bar public and hides all other interfaces:

    { global: foo; bar; local: *; };
    

    See the ld doc at: http://sourceware.org/binutils/docs/ld/VERSION.html#VERSION

    I'm a big advocate of shared libraries, and this ability to limit the visibility of globals is one their great virtues.

    A document that provides more of the advantages of shared objects, but written for Solaris (by Greg Nakhimovsky of happy memory), is at http://developers.sun.com/solaris/articles/linker_mapfiles.html

    I hope this helps.

提交回复
热议问题