Corrade/Utility/VisibilityMacros.h file

Macro CORRADE_VISIBILITY_EXPORT, CORRADE_VISIBILITY_INLINE_MEMBER_EXPORT, CORRADE_VISIBILITY_IMPORT, CORRADE_VISIBILITY_STATIC, CORRADE_VISIBILITY_LOCAL.

Contents

Defines

#define CORRADE_VISIBILITY_EXPORT
Export a symbol into a shared library.
#define CORRADE_VISIBILITY_INLINE_MEMBER_EXPORT
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_VISIBILITY_EXPORT) but dllimport them when using the library (with the CORRADE_VISIBILITY_IMPORT). In practice that means you need to define some additional macro when compiling the library and then #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_VISIBILITY_EXPORT and CORRADE_VISIBILITY_IMPORT macros are defined to the same thing, so the above code works correctly everywhere. If your library can be built as static as well, have a look at CORRADE_VISIBILITY_STATIC.

#define CORRADE_VISIBILITY_INLINE_MEMBER_EXPORT

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::Emitter subclasses) — an inline function defined in a shared library may have a different address inside and outside of that library.

For free functions (and on Windows for member functions in a non-DLL-exported class) CORRADE_VISIBILITY_EXPORT / CORRADE_VISIBILITY_IMPORT can be used without issues, this macro is meant to be used on inline members of an already DLL-exported class, since dllexporting members of already dllexported 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_VISIBILITY_STATIC instead.

#define CORRADE_VISIBILITY_IMPORT

Import a symbol from a shared library.

To be used in tandem with CORRADE_VISIBILITY_EXPORT, see its documentation for more information.

#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_VISIBILITY_EXPORT / CORRADE_VISIBILITY_IMPORT macros in case a library is built as static instead of dynamic.

#define CORRADE_VISIBILITY_LOCAL

Local symbol.

The symbol name will not be exported into a shared or static library. See CORRADE_VISIBILITY_EXPORT for an example.