GCC extension __attribute__

Source: Internet
Author: User
Tags visibility

Transferred from: http://www.th7.cn/Program/cp/2012/03/24/66030.shtml


Imagine this scenario, the program calls a function A,a function exists in two dynamic link library liba.so,libb.so, and the program execution needs to link the two libraries, the program calls a function in the end is from a or B.

This depends on the order of the links, For example, the first link liba.so, this time through the liba.so Export symbol table can find the definition of function A, and added to the symbol table, when the link libb.so, the symbol table already exists function A, will not update the symbol table, so the call is always liba.so a function

The call here is heavily dependent on the order in which the link insecure library is loaded, which may lead to confusion; the GCC extension has the following attribute __attribute__ ((Visibility ("hidden")), which can be used to suppress the name of a function being exported, to the program file that connects to the library, The function is not visible and is used in the following ways:

-fvisibility=default|internal|hidden|protected
The visibility of GCC is that if this property is used during compilation, the symbols of the dynamic library are hidden unless declared.
1. Create a C source file with simple content

#include <stdio.h> #include <stdlib.h> __attribute ((Visibility ("default")) void Not_hidden () {PR   intf ("exported symbol/n");   } void Is_hidden () {printf ("hidden one/n"); }



What you want to do is that the first function symbol can be exported, and the second one is hidden.
First compile into a dynamic library, using the attribute-fvisibility

Gcc-shared-o Libvis.so-fvisibility=hidden VIS.C



View Now

# readelf-s libvis.so |grep hidden 7:0000040c func GLOBAL DEFAULT one Not_hidden 48:00000420 func LOCAL hidden Is_hidden 51:0000040c FUNC GLOBAL DEFAULT one Not_hidden

As you can see, the properties do work.

Now try to link

VI main.c int main () {Not_hidden ();   Is_hidden ();   return 0; }



Attempting to compile into an executable file, linking 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.

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.