[Cmake] Building DLLs on Windows

Geoffrey Cross geoff at cross.lu
Sat Jan 11 19:21:28 EST 2003


In theory it should also be possible to put appropriate 

__declspec(dllimport)

lines in your source code to force the compiler and hence the linker to
export the relevant entry points in your DLL.  However, I never got that
to work (and I was doing exactly the same thing as you), and resorted to
creating .def files:

LIBRARY mymexfilename
EXPORTS mexFunction

Sometimes it seems to be ok to forget the LIBRARY command (which is nice
because it means you only need the one .def file for all your mex
commands), but at some point I found that msdev suddenly started
requiring it, and I never really traced what I did to change its
behavior.

As far as I am aware (and I am admittedly still using CMake 1.4), if you
just put a .def file in as an additional source file for your dll build,
CMake will do the appropriate thing.  

So in 1.4-speak, my CMakeLists look like this, and work (i.e. generate
mex libraries which work just fine) both under WIN32 and under Unix
(I've only tried Linux):

# matlab specific stuff
IF( WIN32 )
  INCLUDE_DIRECTORIES( path_to_matlab/extern/include )
  LINK_DIRECTORIES( path_to_matlab/extern/lib/win32/microsoft/msvc60 )
  LINK_LIBRARIES( libmx.lib libmex.lib )
ELSE( WIN32 )
  INCLUDE_DIRECTORIES( path_to_matlab/extern/include )
  LINK_DIRECTORIES( path_to_matlab/bin/glnx86 )
  LINK_LIBRARIES( mx mex )
ENDIF( WIN32 )

# library to be built
ADD_LIBRARY(mexprim SHARED prim_sources)

# source files for the executables
SOURCE_FILES(prim_sources
prim.cxx
matlab.def
)

It seems that the mexVersion symbol isn't actually needed, even though
the matlab spec says it is.  And I started to sort out the issues you
commented on the other day with the renaming of the built libraries, but
got bored and wrote myself a shell script to do it :).   Similarly, I
hard-wired my matlab distribution, but next time I have a few minutes to
spare I'll use your FindMATLAB script (thanks!).

Geoff.



> -----Original Message-----
> From: cmake-admin at public.kitware.com [mailto:cmake-
> admin at public.kitware.com] On Behalf Of gareth.jones at stud.man.ac.uk
> Sent: Saturday, January 11, 2003 10:06 PM
> To: cmake at public.kitware.com
> Subject: [Cmake] Building DLLs on Windows
> 
> I've been getting CMake to setup Visual Studio builds for MEX files
> (which are just specialised DLLs).  My understanding of Windows is
> that when you build a DLL you must specify the symbols that it exports
> and the only way I know is to give a .DEF file and specify it as an
> argument to the linker: `/DEF:C:/full/path/to/file.DEF'.  As far as I
> can see CMake doesn't include any specific functionality for doing
> this, but if anyone can point it out to me I'd be grateful.
> 
> Since the only symbol that a MEX file exports is mexFunction
> (mexFunction at 16 for Fortran) I can create the .DEF file using
> WRITE_FILE, but passing the flag to the linker is difficult.  Using
> CMAKE_SHARED_LINKER_FLAGS is not good because each MEX file needs a
> different .DEF file, and thus a different linker argument.  Actually,
> they will work with the same file but this causes a linker warning; on
> the other hand, adding support for LINK_FLAGS for Visual Studio solves
> the problem completely and the change is trivial:
> 
> --- Source/cmLocalVisualStudio6Generator.cxx	17 Dec 2002 17:56:04
-0000
> 	1.13
> +++ Source/cmLocalVisualStudio6Generator.cxx	11 Jan 2003 20:12:26
-0000
> @@ -787,6 +787,16 @@
>      libMultiLineOptions += " \n";
>      }
> 
> +  if(const char* targetLinkFlags = target.GetProperty("LINK_FLAGS"))
> +    {
> +    libOptions += " ";
> +    libOptions += targetLinkFlags;
> +    libOptions += " ";
> +    libMultiLineOptions += "# ADD LINK32 ";
> +    libMultiLineOptions +=  targetLinkFlags;
> +    libMultiLineOptions += " \n";
> +    }
> +
>    // are there any custom rules on the target itself
>    // only if the target is a lib or exe
>    std::string customRuleCode = this->CreateTargetRules(target,
libName);
> 
> So I was wondering if something like this could be added to CMake,
> please?  I assume something similar is needed for Visual Studio 7 but
> I can't test that.
> 
> The other problem I have is where to generate the .DEF files.  Is the
> target_CMAKE_PATH variable the recommended way of referring to the
> directory which will contain target's object files?
> 
> Incidently, I found a minor documentation bug for the FIND_PROGRAM
> command -- NOTFOUND is half-described but not implemented which
> confused me.
> 
> Thanks,
> 
> Gareth Jones
> 
> PS: I've put my FindMatlab.cmake script up at
> <http://www.isbe.man.ac.uk/FindMatlab.cmake> if anyone wants to see
> it.  I'd be very grateful for any comments on how to improve it.
> _______________________________________________
> Cmake mailing list
> Cmake at public.kitware.com
> http://public.kitware.com/mailman/listinfo/cmake





More information about the CMake mailing list