[Cmake] Building DLLs on Windows

gareth.jones@stud.man.ac.uk gareth . jones at stud . man . ac . uk
11 Jan 2003 22:05:42 +0000


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.