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
From 19b63c2b3f89c44ccfe5f0b7e7ef8aa2ea6377a1 Mon Sep 17 00:00:00 2001
From: Roger Leigh <[email protected]>
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@_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@_TEMPLATE
 #    else
         /* We are using this library */
 #      define @EXPORT_MACRO_NAME@ @DEFINE_IMPORT@
+#      define @EXPORT_MACRO_NAME@_TEMPLATE extern
 #    endif
 #  endif
 
-- 
2.5.0

-- 

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at 
http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake-developers

Reply via email to