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