A.3. Exporting Library Symbols

If you want to implement classes for your module or your module itself in such a way that other projects can also link their interfaces, you have to care about that in a particular way in order to make sure that these types, classes and symbols are available even on Windows systems. For doing this correctly, all symbols other libraries use must be exported; this is which is generally solved in ML projects by writing a macro in front of your (e.g.) class containing the export code. The macro is normally defined in the InitSystem.h file of your project where the platform-dependent stuff is implemented. The macro is usually something like this:

Example A.1. Exporting Library Symbols

//------ Solve platform dependent symbol exporting with macros --------------
#ifdef WIN32
#ifdef MLEXAMPLE_EXPORTS
// To make functions, classes and other symbols available
// on this DLL interfaces, they must be exported explicitly
// on win32 systems. We add simply MLEXAMPLE_EXPORT before
// them.
#define MLEXAMPLE_EXPORT __declspec(dllexport)
#else
//! When included by other libraries MLEXAMPLE_EXPORT is
//! compiled as import symbol.
#define MLEXAMPLE_EXPORT __declspec(dllimport)
#endif

#else
// Non windows systems:
//! Exporting library symbols is not used on non windows systems.
#define MLEXAMPLE_EXPORT
#endif  

Class export is done with a code like this:

class MLEXAMPLE_EXPORT AddExample : public Module{
 ...
}

MLEXAMPLE_EXPORTS is defined in the project CMakeLists.txt file; on Windows platforms, all classes of the project, for example, will implement __declspec(dllexport) in front of the symbol so that it will be available on the library interface. Other projects that do not define MLEXAMPLE_EXPORTS will implement __declspec(dllimport) to mark the symbol as linked from another library.

Such symbols are not defined on non-Windows platforms, i.e., the MLEXAMPLE_EXPORT will have no effect on Linux, for example, because it is simply not compiled.