[CMake] Building either shared or static version of a library on Windows
Michael Jackson
mike.jackson at bluequartz.net
Tue Jan 15 11:31:43 EST 2013
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.
___________________________________________________________
Mike Jackson Principal Software Engineer
BlueQuartz Software Dayton, Ohio
mike.jackson at bluequartz.net www.bluequartz.net
On Jan 15, 2013, at 11:09 AM, Bogdan Cristea wrote:
> Hi
>
> My exports header file for a C/C++ project has the following
> definitions:
>
> #if defined(WIN32) || defined(WIN64)
> #ifndef LIB_EXPORT
> #ifdef lib_EXPORTS
> #define LIB_EXPORT __declspec(dllexport)
> #else
> #define LIB_EXPORT __declspec(dllimport)
> #endif
> #endif
> #endif
>
> where lib_EXPORTS is automatically defined by cmake when the shared
> library is build. However, this aproach works only with a shared
> library, when I try to build the static version I get a compilation
> error with Visual Studio 2010, since LIB_EXPORT is
> __declspec(dllimport).
>
> An workaround is to define in the CMakeLists.txt file another compiler
> variable when the static version is build and set in the exports header
> LIB_EXPORT to an empty define when the static library is build. However,
> this must be done each time a program is build with the static version
> of the library, which makes this approach cumbersome.
>
> Is there a better way to define the exports header file in order to be
> able to build and compile with other program either the shared or the
> static version of a library ?
>
> thanks
> Bogdan
>
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
More information about the CMake
mailing list