The compiler supports nested visibility pragma preprocessor directives. If entities are included in several pairs of the nested #pragma GCC visibility push and #pragma GCC visibility pop directives, the nearest pair of directives takes effect. See Example 1.
You must not specify the visibility pragma directives for header files. Otherwise, your program might exhibit undefined behaviors. See Example 2.
Example 1
In this example, the function and variables have the visibility attributes that are specified by their nearest pairs of pragma preprocessor directives.#pragma GCC visibility push(default)
namespace ns
{
void vis_f_fun() {} //default
# pragma GCC visibility push(internal)
int vis_v_i; //internal
# pragma GCC visibility push(protected)
int vis_v_j; //protected
# pragma GCC visibility push(hidden)
int vis_v_k; //hidden
# pragma GCC visibility pop
# pragma GCC visibility pop
# pragma GCC visibility pop
}
#pragma GCC visibility pop
Example 2
In this example, the compiler issues a link error message to indicate that the definition of the printf() library function cannot be found.#pragma GCC visibility push(hidden)
#include <stdio.h>
#pragma GCC visibility pop
int main(){
printf("hello world!");
return 0;
}