When using CMake's GenerateExportHeader on Linux/macOS, it generates something like this by default for shared libraries:
# ifndef SOMELIB_EXPORT
# ifdef somelib_EXPORTS
/* We are building this library */
# define SOMELIB_EXPORT __attribute__((visibility("default")))
# else
/* We are using this library */
# define SOMELIB_EXPORT __attribute__((visibility("default")))
# endif
# endif
Using __attribute__((visibility("default"))) in the "We are building this library" section is important. It ensures that public symbols will be visible in the library.
But does __attribute__((visibility("default"))) do anything at all in the "We are using this library" section? My understanding is that the visibility of symbols is already set when building the library, and cannot be changed when using it. Thus, the visibility declaration is completely unnecessary when using the library. Is this correct?
(Note: I understand that on Windows SOMELIB_EXPORT would be defined as __declspec(dllimport), which is in fact necessary when using the library. However, this question concerns solely Linux and macOS, not Windows.)