[CMake] Building either shared or static version of a library on Windows

Alexander Neundorf a.neundorf-work at gmx.net
Wed Jan 16 14:31:12 EST 2013


On Tuesday 15 January 2013, Michael Jackson wrote:
> There are lots of different ways to do this. The following is an example of
> what *I* do. ----- In my CMakeLists.txt file I have this
>  set(DREAM3DLib_BUILT_AS_DYNAMIC_LIB)
> IF (BUILD_SHARED_LIBS)
>     set(DREAM3DLib_BUILT_AS_DYNAMIC_LIB 1)
>     IF (WIN32)
>         ADD_DEFINITIONS("-DDREAM3DLib_BUILT_AS_DYNAMIC_LIB")
>     ENDIF (WIN32)
> ENDIF (BUILD_SHARED_LIBS)
> 
> ----- In a Header file Define this
> 
>  /* Cmake will define DREAM3DLib_EXPORTS on Windows when it
> configures to build a shared library. If you are going to use
> another build system on windows or create the visual studio
> projects by hand you need to define DREAM3DLib_EXPORTS when
> building the MXADatModel DLL on windows.
> */
> 
> #if defined (DREAM3DLib_BUILT_AS_DYNAMIC_LIB)
> 
>   #if defined (DREAM3DLib_EXPORTS)  /* Compiling the MXA DLL/Dylib */
>     #if defined (_MSC_VER)  /* MSVC Compiler Case */
>       #define  DREAM3DLib_EXPORT __declspec(dllexport)
>       #define EXPIMP_TEMPLATE
>     #elif (__GNUC__ >= 4)  /* GCC 4.x has support for visibility options */
>       #define DREAM3DLib_EXPORT __attribute__ ((visibility("default")))
>     #endif
>   #else  /* Importing the DLL into another project */
>     #if defined (_MSC_VER)  /* MSVC Compiler Case */
>       #define  DREAM3DLib_EXPORT __declspec(dllimport)
>       #define EXPIMP_TEMPLATE extern
>     #elif (__GNUC__ >= 4)  /* GCC 4.x has support for visibility options */
>       #define DREAM3DLib_EXPORT __attribute__ ((visibility("default")))
>     #endif
>   #endif
> #endif
> 
> /* If DREAM3DLib_EXPORT was never defined, define it here */
> #ifndef DREAM3DLib_EXPORT
>   #define DREAM3DLib_EXPORT
>   #define EXPIMP_TEMPLATE
> #endif


you may want to have a look at GenerateExportHeader.cmake, which comes with 
cmake since a few releases. This helps creating those defines etc.

Alex


More information about the CMake mailing list