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

Bogdan Cristea cristeab at gmail.com
Tue Jan 15 11:09:56 EST 2013


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



More information about the CMake mailing list