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

Bogdan Cristea cristeab at gmail.com
Wed Jan 16 03:24:06 EST 2013


Le mardi 15 janvier 2013 à 11:56 -0500, Michael Jackson a écrit :
> 
> On Jan 15, 2013, at 11:44 AM, Bogdan Cristea wrote:
> 
> > 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
> > 
> 
> Right, so I also have issues like this when I bring in some other libraries, for example HDF5. In my own implementation of FindHDF5.cmake I specifically look for a variable "HDF5_BUILT_AS_DYNAMIC_LIB" and then create a header file that has this:
> 
> #define HDF5_BUILT_AS_DYNAMIC_LIB 0
> OR 
> #define HDF5_BUILT_AS_DYNAMIC_LIB 1
> 
> depending on what came back from the test. You could do the same thing for your library. Create a header file with the proper defines that gets shipped with your library. Users can test for that symbol if they are NOT using CMake. If they *are* using CMake then you can also ship some CMake files that already have the proper CMake variables defined in the proper way for your library was built. This allows the user of your library to "do the right" thing by simply #include <your_header.h> which will have the proper define.
> 
> Hope that helps:

Yes, this seems to be the solution.




More information about the CMake mailing list