file
VisibilityMacros.hMacro CORRADE_
Defines
- #define CORRADE_VISIBILITY_EXPORT
- Export a symbol into a shared library.
- #define CORRADE_VISIBILITY_INLINE_MEMBER_EXPORT new in 2019.10
- Export inline class member into a shared library.
- #define CORRADE_VISIBILITY_IMPORT
- Import a symbol from a shared library.
- #define CORRADE_VISIBILITY_STATIC
- Public symbol in a static library.
- #define CORRADE_VISIBILITY_LOCAL
- Local symbol.
Define documentation
#define CORRADE_VISIBILITY_EXPORT
Export a symbol into a shared library.
On non-DLL platforms, if the GCC/Clang -fvisibility=hidden
flag is used (recommended, enabled by default when you use the CORRADE_USE_PEDANTIC_FLAGS
CMake property), symbols are not exported from shared libraries by default and you need to mark them as such in order to make them usable from outside code. For example:
void privateFunction(); /* can't be used outside of the shared library */ CORRADE_VISIBILITY_EXPORT void exportedFunction(); class CORRADE_VISIBILITY_EXPORT ExportedClass { public: void foo(); /* Non-inline members get implicitly exported as well */ private: /* Used only privately, thus doesn't need to be exported */ CORRADE_VISIBILITY_LOCAL void privateFoo(); };
On Windows when building DLLs, this gets complicated further — everything is implicitly hidden and you need to dllexport
the symbols when building the library (using the CORRADE_dllimport
them when using the library (with the CORRADE_#define
an alias to one of these based on presence of that macro. If you use CMake, such macro is implicitly provided in a form of <target>_EXPORTS
when compiling any file that's a part of a shared library, so then the full setup might look like this:
add_library(MyLibrary SHARED MyLibrary.cpp) # MyLibrary_EXPORTS defined add_executable(my-application main.cpp) # MyLibrary_EXPORTS not defined target_link_librariees(my-application PRIVATE MyLibrary)
#ifdef MyLibrary_EXPORTS #define MYLIBRARY_EXPORT CORRADE_VISIBILITY_EXPORT #else #define MYLIBRARY_EXPORT CORRADE_VISIBILITY_IMPORT #endif class MYLIBRARY_EXPORT ExportedClass { public: // … };
On non-DLL platforms both CORRADE_
#define CORRADE_VISIBILITY_INLINE_MEMBER_EXPORT new in 2019.10
Export inline class member into a shared library.
If the GCC/Clang -fvisibility-inlines-hidden
flag is used (enabled by default when you use the CORRADE_USE_PEDANTIC_FLAGS
CMake property), inline
functions and class methods are not exported. This is generally a good thing but may cause issues when you need to compare pointers to such inline functions (for example signals defined in Interconnect::
For free functions (and on Windows for member functions in a non-DLL-exported class) CORRADE_dllexport
ing members of already dllexport
ed type is not allowed:
class MYLIBRARY_EXPORT ExportedClass { public: // … CORRADE_VISIBILITY_INLINE_MEMBER_EXPORT int inlineFoo() { return stuff + 3; } };
This macro is empty on Windows, which means there's no need for a corresponding *_IMPORT
variant of it. Note that this macro exports the symbol even on static libraries, in that case you might want to use CORRADE_
#define CORRADE_VISIBILITY_IMPORT
Import a symbol from a shared library.
To be used in tandem with CORRADE_
#define CORRADE_VISIBILITY_STATIC
Public symbol in a static library.
Defined as empty — to be consistent with hidden visibility by default, symbols in static libraries shouldn't be exported either. This macro is provided mainly as a self-documenting alternative to the CORRADE_
#define CORRADE_VISIBILITY_LOCAL
Local symbol.
The symbol name will not be exported into a shared or static library. See CORRADE_