Specifying Symbol Visibility Explicitly

You can explicitly set the visibility of an individual symbol using the visibility attribute on a data or function declaration.  For example:

int i __attribute__ ((visibility("default")));

void __attribute__ ((visibility("hidden"))) x () {...}

extern void y() __attribute__ ((visibilty("protected");

The visibility declaration attribute accepts one of the five keywords:

The value of the visibility declaration attribute overrides the default set by the -fvisibility, -fpic, or -fno-common attributes.

If you have a number of symbols for which you wish to specify the same visibility attribute, you can set the visibility using one of the five command line options:

where file  is the pathname of a file containing a list of the symbol names whose visibility you wish to set.  The symbol names in the file are separated by white space (blanks, TAB characters, or newlines).  For example, the command line option:

-fvisibility-protected=prot.txt

where file prot.txt contains:

a

b c d

  e

sets protected visibility for symbols a, b, c, d, and e.  This has the same effect as

__attribute__ ((visibility=("protected")))

on the declaration for each of the symbols.  Note that these two ways to explicitly set visibility are mutually exclusive--you may use __attribute((visibilty())) on the declaration, or specify the symbol name in a file, but not both.

You can set the default visibility for symbols using one of the command line options:

This option sets the visiblity for symbols not specified in a visibility list file and that do not have __attribute__((visibilty())) in their declaration. For example, the command line options:

-fvisibility=protected -fvisibility-default=prot.txt

where file prot.txt is as previously described, will cause all global symbols except a, b, c, d, and e to have protected visibility.  Those five symbols, however, will have default visibility and thus be preemptable.