[Cmake] Building DLLs

Bill Hoffman bill.hoffman at kitware.com
Wed Mar 19 09:20:09 EST 2003


Windows dll's require a bit more magic than .so libraries on unix.
You have to explicitly tell the linker what symbols you want to export
from the dll.    You can do this in two ways:


1. This can be done with a .def file which contains a text
listing of all of the symbols you want to export.   A .def file can be
added to the source list for a library and cmake will use it.   
Something like this:
EXPORTS
    adler32                        @1
    compress                       @2


2. Change the source code and add declspec(dllexport) in front
of the symbols you want exported with an ifdef around them.

Something like this:

 #if defined(vtkCommon_EXPORTS)
  #define VTK_COMMON_EXPORT __declspec( dllexport )
 #else
  #define VTK_COMMON_EXPORT __declspec( dllimport )
 #endif


class VTK_COMMON_EXPORT vtkObject : public vtkObjectBase
{
...
}

CMake will automatically add the -Dlibname_EXPORTS to the command line
when building a dll.


It is possible to automatically create the .def file, but I have not
yet added this feature to cmake.  It involves running all the .obj files
through dumpbin (like nm for windows), and creating a .def file from the
output.    However, it would be different for borland and other compilers that
do not use dumpbin.    Also, the declspec stuff ends up being more efficent.

Also, the .dll file is only for run time.   At link time, the linker looks
for a file with the .lib extension that contains the exports for the .dll run
time file.   

-Bill

At 03:42 AM 3/19/2003, Ian Main wrote:

>So, thanks to all your help I got roy building on unix now all spiffy
>like!
>
>Now, I've moved onto getting it working on windows.  I've been building
>on win2k using MSVC++ 6.0.
>
>I can build it fine with static libs, however, I added:
>
>SET(BUILD_SHARED_LIBS "ON" CACHE BOOL "Turn on to build shared libraries.")
>
>So that I could build shared libraries.. If I enable this on windows though
>I get problems.  My setup is actually a lot like the example Hello one.  I have
>libroy building along with some other libaries and some tests that need to link
>to it.  With shared libs on I get:
>
>cannot open file "roy.lib"
>
>So obviously it is still trying to link against the static version.
>
>Another odd thing happens when I try to use the DLL for zlib.  I select it in the
>configuration and I get:
>
>cannot open file "E:\cvs\zlib\dll32\zlib.dll.lib"
>
>Now I'm probly missing something, and I know there's a lot more magic involved
>with DLLs than with .so's..
>
>Any Ideas?
>
>Oh, I gather it's not possible to build both shared and static libs?  That's
>generally what us unix folk do, though I'm not sure why I suppose :)
>
>    Thanks,
>
>            Ian
>
>_______________________________________________
>Cmake mailing list
>Cmake at public.kitware.com
>http://public.kitware.com/mailman/listinfo/cmake 






More information about the CMake mailing list