The method of controlling export function of GCC compilation shared library under Linux

Source: Internet
Author: User
Tags visibility

Through the development of some practical projects, it is found that under Windows, you can control which functions in the DLL (dynamic-link library) can be exported by specifying __declspec (dllexport) definitions, which are exposed to other program links, and which functions are used internally by DLLs. , and in Linux does not exist dllexport such an indicator, by default, GCC compiled so (shared library) when the code all the functions are exported, then how to implement the effect of Windows, we control the shared library export function?

The

actually has a similar control mechanism under Linux. In the GCC help documentation, there is a description of the-fvisibility=default|internal|hidden|protected parameter and its value:
A superior solution made possible by this option to marking things hidden when the default was public is, the default hidden and mark things public. The norm with DLL's on Windows and With-fvisibility=hidden and "__attribute__ ((Visibility (" default "))" instead O F "__declspec (dllexport)" You get almost identical semantics with identical syntax. This was a great boon to those working with cross-platform projects. What the
needs to know is that under Linux, all the functions in the source file have the visibility property set to public by default, and when the-fvisibility=hidden parameter is added to the compilation command, all the default public properties are changed to hidden. At this point, if you set the __attribute__ ((visibility ("default)") parameter to the function, the function is handled by the public property, and the-fvisibility=hidden parameter does not work on the function. So, with the-fvisibility=hidden parameter set for GCC, only functions that have the __attribute__ ((Visibility ("Default")) property set are visible externally, so the effect is equivalent to the Visual The __declspec (dllexport) definition under Studio. For example:

extern int foo (int a, int b, int c) __attribute__ ((Visibility ("default"));

You can declare this.

The method of controlling export function of GCC compilation shared library under Linux

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.