[cmake-developers] [patch] Export template instantiations with GenerateExportHeader

Roger Leigh rleigh at codelibre.net
Thu Jul 30 11:23:03 EDT 2015


See attached patch.

This is following the recommendations here: 
https://support.microsoft.com/en-us/kb/168958

While the existing foo_EXPORT provided by GenerateExportHeader is fine 
for use in exporting functions and classes, it isn't apparently 
sufficient for template export.  This is needed, for example, if you 
wish to use templated types in exported classes as members.  In this 
case you need to use "extern" when importing the exported template, and 
this requires a second macro.  For example, to export std::string and 
std::vector<std::string>:

foo_EXPORT_TEMPLATE template class foo_EXPORT std::allocator<char>;
foo_EXPORT_TEMPLATE template class foo_EXPORT std::basic_string<char, 
std::char_traits<char>, std::allocator<char> >;
foo_EXPORT_TEMPLATE template class foo_EXPORT 
std::allocator<std::basic_string<char, std::char_traits<char>, 
std::allocator<char> > >;
foo_EXPORT_TEMPLATE template class foo_EXPORT 
std::vector<std::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, std::allocator<std::basic_string<char, 
std::char_traits<char>, std::allocator<char> > > >;

With this done, it can then be used in an exported class:

class foo_EXPORT foo
{
   std::vector<std::string> val;

  public:
   foo();

   std::vector<std::string> const&
   getval() const;
};


Note that I'm by no means a Windows C++ or DLL expert--I'm primarily a 
C++ UNIX developer--this is just from reading the docs and experimenting.

An example project making use of this is here: 
https://github.com/rleigh-dundee/dlltest
(with unpatched cmake--I create the macros after using the generated 
export header)


Regards,
Roger
-------------- next part --------------
From 19b63c2b3f89c44ccfe5f0b7e7ef8aa2ea6377a1 Mon Sep 17 00:00:00 2001
From: Roger Leigh <r.leigh at dundee.ac.uk>
Date: Thu, 30 Jul 2015 15:47:38 +0100
Subject: [PATCH] GenerateExportHeader: Add EXPORT_TEMPLATE macro

In addition to the foo_EXPORT macro, a foo_EXPORT_TEMPLATE
macro is also generated.  This is to allow for explicit
export of templates in Windows DLLs following Microsoft's
guidance here: https://support.microsoft.com/en-us/kb/168958
---
 Modules/exportheader.cmake.in | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/Modules/exportheader.cmake.in b/Modules/exportheader.cmake.in
index 118de16..f442e98 100644
--- a/Modules/exportheader.cmake.in
+++ b/Modules/exportheader.cmake.in
@@ -4,15 +4,18 @@
 
 #ifdef @STATIC_DEFINE@
 #  define @EXPORT_MACRO_NAME@
+#  define @EXPORT_MACRO_NAME at _TEMPLATE
 #  define @NO_EXPORT_MACRO_NAME@
 #else
 #  ifndef @EXPORT_MACRO_NAME@
 #    ifdef @EXPORT_IMPORT_CONDITION@
         /* We are building this library */
 #      define @EXPORT_MACRO_NAME@ @DEFINE_EXPORT@
+#      define @EXPORT_MACRO_NAME at _TEMPLATE
 #    else
         /* We are using this library */
 #      define @EXPORT_MACRO_NAME@ @DEFINE_IMPORT@
+#      define @EXPORT_MACRO_NAME at _TEMPLATE extern
 #    endif
 #  endif
 
-- 
2.5.0



More information about the cmake-developers mailing list