There is a better way than @Wakely. Do it like this:
#ifdef MYLIB_DLL #ifndef MYLIB_IFACE #ifdef MYLIB_IFACE_EXPORT #define MYLIB_IFACE _declspec( dllexport ) #else // !MYLIB_IFACE_EXPORT #define MYLIB_IFACE _declspec( dllimport ) #endif // !MYLIB_IFACE_EXPORT #endif // !MYLIB_IFACE #else // !MYLIB_DLL #ifndef MYLIB_IFACE #define MYLIB_IFACE #endif // !MYLIB_IFACE
Put such a block in the header that is used by each file in your DLL, and in the public header for your DLL.
Each character that should be exported from your DLL will be marked as follows:
class MYLIB_IFACE MyClass { }; void MYLIB_IFACE myFunc();
Then in each .cpp file in your dll, the first line should be:
#define MYLIB_IFACE_EXPORT
If you do this, it will work perfectly on POSIX systems that do not use dllexport / dllimport. To create the DLL version of your library, you define MYLIB_DLL. (you can do this in compiler flags so that it can be controlled from your build system)
To create a static version of your library, do not define MYLIB_DLL.
@Update:
You can expand this to maintain GCC visibility as follows:
#ifdef WIN32 #define KX_SYMBOL_EXPORT _declspec( dllexport ) #define KX_SYMBOL_IMPORT _declspec( dllimport ) #else // GCC #define KX_SYMBOL_EXPORT __attribute__(( visibility ("default"))) #define KX_SYMBOL_IMPORT #endif #ifdef KX_DLL #ifndef KX_IFACE #ifdef KX_IFACE_EXPORT #define KX_IFACE KX_SYMBOL_EXPORT #else // !KX_IFACE_EXPORT #define KX_IFACE KX_SYMBOL_IMPORT #endif // !KX_IFACE_EXPORT #endif // !KX_IFACE #else // !KX_DLL #ifndef KX_IFACE #define KX_IFACE #endif // !KX_IFACE #endif // !KX_DLL
I remove the GCC bit in the first example for simplicity. But this is true. @Wakely is so right.
source share