__ATTRIBUTE__ ((Visibility ("default"))

Source: Internet
Author: User
Tags visibility

Jason1512visibility use of gcc (ZZ)-fvisibility=default|internal|hidden|protected
The visibility of GCC is that if this attribute is used at compile time, then the symbols of the dynamic library are hidden unless forced to declare.



1. Create a C source file with simple content
#include <stdio.h>
#include <stdlib.h>


__attribute ((Visibility ("default")) void Not_hidden ()
{
printf ("Exported symbol\n");
}

void Is_hidden ()
{
printf ("Hidden one\n");
}

Want to do is, the first function symbol can be exported, the second is hidden.
First compiled into a dynamic library, using the attribute-fvisibility
Gcc-shared-o Libvis.so-fvisibility=hidden VIS.C

Now view
# readelf-s libvis.so |grep Hidden
7:0000040c FUNC GLOBAL DEFAULT Not_hidden
48:00000420 FUNC LocalHIDDENOne Is_hidden
51:0000040c FUNC GLOBAL DEFAULT Not_hidden
As you can see, the properties do work.

Now trying to link
VI main.c
int main ()
{
Not_hidden ();
Is_hidden ();
return 0;
}

Trying to compile into an executable file, link to the dynamic library just generated,
Gcc-o exe main.c-l./-lvis
Results hint:

/tmp/cckythcl.o:in function ' main ':
MAIN.C: (. text+0x17): Undefined reference to ' Is_hidden '

It shows that hidden does play a role. #c/c++

Attribute definitions specific to g++

As a supplement to the Visibility attribute (see "Visibility Attributes and Pragmas for GCC C + + Libraries"), g++ provides 2 additional attributes, namely init_priority ( Priority) and Java_interface properties.

The init_priority Attribute

This property allows the user to control the initialization order of objects in a namespace. Typically, objects are initialized in the order in which they are defined in a unit of code. Init_priority has only one integer parameter, the value is 101 to 65535, the smaller the higher the precedence. For example, in the following pseudo code, class MyClass will be initialized first than class YourClass:

Class MyClass

{

...

};

Class YourClass

{

__ATTRIBUTE__ ((Visibility ("default")) void MyMethod ();

...

};

To change the order in which they are initialized, you can convert the code to the following:

Class MyClass

{

__ATTRIBUTE__ ((init_priority (65535)));

...

};

Class YourClass

{

__ATTRIBUTE__ ((init_priority (101)));

...

};

You only need to pay attention to the priority series value of the order, the specific use of which value does not matter (that is, as long as the MyClass priority value than the YourClass, is not 65535 and 101 do not matter).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.