[CMake] Building shared libs on visual

Michael Jackson mike.jackson at bluequartz.net
Wed Sep 24 07:59:19 EDT 2008


On Sep 24, 2008, at 5:47 AM, Julien Michel wrote:

> Werner Smekal a écrit :
>> Hi,
>> maybe you forgot to export symbols?
>> http://www.cmake.org/Wiki/BuildingWinDLL
>
> Dear Werner,
>
> Thank you, I did search the cmake Wiki for something like that, but I
> did not manage to find this page. I think we are exporting/importing  
> our
> symbols in most of the case, but I did not know that visual will  
> simply
> not build any dll if there is no exported symbol.
>
> This lead me to another question:
> In the wiki page, it is suggested to do the following:
>
> #if defined (_WIN32)
>  #if defined(MyLibrary_EXPORTS)
>    #define  MYLIB_EXPORT __declspec(dllexport)
>  #else
>    #define  MYLIB_EXPORT __declspec(dllimport)
>  #endif /* MyLibrary_EXPORTS */
> #else /* defined (_WIN32) */
> #define MYLIB_EXPORT
> #endif
>
> But in this piece of code, the case were we build static libraries  
> under
> windows is not handled. This means that we will import/export symbols
> even if we build static libraries, and this seems to break the
> compilation, leading to "undefine reference to declspec(dllimport)  
> xxx"
> when trying to link against the library. I did look at the export
> sections of other libraries and they seem to handle this static case  
> by
> removing the __declspec(dllexport)/__declspec(dllimport). What is the
> right thing to do here ?
>
> Thank you for your reply,
> Best regards,
>
> Julien
> -- 

Julien,
  Funny. I wrote that wiki entry and it was a bit different when I  
first wrote it but after some feedback I  changed the code slightly  
and looking back at the code I don't think it would work correctly for  
the static case.
     Like Werner I actually use a second definition in my code:
#if defined (_WIN32) && defined (BUILT_AS_DYNAMIC_LIB)
#if defined(MyLibrary_EXPORTS)
#define  MYLIB_EXPORT __declspec(dllexport)
#else
#define  MYLIB_EXPORT __declspec(dllimport)
#endif /* MyLibrary_EXPORTS */
#else /* defined (_WIN32) */
#define MYLIB_EXPORT
#endif
#endif /* _MyLibrary_DLLDEFINES_H_ */

Then in the cmake code you do something like:
# Allow the developer to select if Dynamic or Static libraries are built
OPTION (BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
# Set the LIB_TYPE variable to STATIC
SET (LIB_TYPE STATIC)
IF (BUILD_SHARED_LIBS)
   # User wants to build Dynamic Libraries, so change the LIB_TYPE  
variable to CMake keyword 'SHARED'
   ADD_DEFINITIONS(-DBUILD_AS_DYNAMIC_LIBRARY)
   SET (LIB_TYPE SHARED)
ENDIF (BUILD_SHARED_LIBS)

Which should help take care of that case for you.

Mike





More information about the CMake mailing list