Rules of visibility attributes (IBM extension)

Rules of determining the visibility attributes

The visibility attribute of an entity is determined by the following rules:
  1. If the entity has an explicitly specified visibility attribute, the specified visibility attribute takes effect.
  2. Otherwise, if the entity has a pair of enclosing pragma directives, the visibility attribute that is specified by the pragma directives takes effect.
  3. Otherwise, the setting of the -qvisibility option takes effect.
Note: On the AIX® platform, if the setting of the -qvisibility option takes effect and is specified to unspecified (the default value), the entity does not have a visibility attribute.

Rules and restrictions of using the visibility attributes

When you specify visibility attributes for entities, consider the following rules and restrictions:
  • You can specify visibility attributes only for entities that have external linkage. The compiler issues a warning message when you set the visibility attribute for entities with other linkages, and ignore the specified visibility attribute. See Example 4.
  • You cannot specify different visibility attributes in the same declaration or definition of an entity; otherwise, the compiler issues an error message. See Example 5.
  • If an entity has more than one declaration that is specified with different visibility attributes, the visibility attribute of the entity is the first visibility attribute that the compiler processes. See Example 6.
  • You cannot specify visibility attributes in the typedef statements. See Example 7.

Example 4

In this example, the compiler ignores the visibility attributes of variables m, i, and j, because m and i have internal linkage, and j has no linkage.
static int m __attribute__((visibility("protected"))); 
int n __attribute__((visibility("protected")));

int main(){
   int i __attribute__((visibility("protected"))); 
   static int j __attribute__((visibility("protected)));
}

Example 5

In this example, the compiler issues an error message to indicate that you cannot specify two different visibility attributes at the same time in the definition of variable m.
//error
int m __attribute__((visibility("hidden"))) __attribute__((visibility("protected"))); 

Example 6

In this example, the first declaration of function fun() that the compiler processes is extern void fun() __attribute__((visibility("hidden"))), so the visibility attribute of fun() is hidden.
extern void fun() __attribute__((visibility("hidden")));
extern void fun() __attribute__((visibility("protected"))); 

int main(){
   fun();
}

Example 7

In this example, the visibility attribute of variable vis_v_ti is default, which is not affected by the setting in the typedef statement.
//The -qvisibility=default option is specified.
typedef int __attribute__((visibility("protected"))) INT;
INT vis_v_ti = 1;