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

Bogdan Cristea cristeab at gmail.com
Tue Jan 15 11:44:31 EST 2013


Le mardi 15 janvier 2013 à 11:31 -0500, Michael Jackson a écrit :
> 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
> 
> Hope that helps. There is also a wiki entry on this topic on the CMake
> Wikie. "Building a DLL for Linux Devs" which might help also. 

Hi

Thank you for your quick answer. This approach still leaves me with one
problem: each time I need to use the dynamic version of the library for
building another program I need to remember to define
DREAM3DLib_BUILT_AS_DYNAMIC_LIB in my CMakeLists.txt file, which is
exactly what I would like to avoid.

regards
Bogdan



More information about the CMake mailing list