title: Generate all special members of a class while building shared library
body: <p>I am trying to build a shared library and want to export only the relevant symbols from it. Any private implementation details should not be exported.</p>
  <p>This however fails when it comes to any of the special member functions like the default constructor as illustrated by the following sample project:</p>
  <p>CMakeLists.txt:</p>
  <pre><code>cmake_minimum_required (VERSION 3.12)
  project (visibility)
  
  include (GenerateExportHeader)
  
  add_library (library SHARED
      include/detail/impl.hpp
      include/library.hpp
      src/detail/impl.cpp
      src/library.cpp
  )
  
  set_target_properties (library PROPERTIES CXX_VISIBILITY_PRESET &quot;hidden&quot;)
  
  target_include_directories (library PUBLIC
      $&lt;BUILD_INTERFACE:${visibility_SOURCE_DIR}/include&gt;
      $&lt;BUILD_INTERFACE:${visibility_BINARY_DIR}/include&gt;
  )
  
  generate_export_header (library EXPORT_FILE_NAME
      ${visibility_BINARY_DIR}/include/export.hpp
  )
  
  add_executable (test test/test.cpp)
  target_link_libraries (test PRIVATE library)
  </code></pre>
  <p>impl.hpp:</p>
  <pre><code>#include &lt;export.hpp&gt;
  
  struct LIBRARY_NO_EXPORT Impl {
    Impl();
  };
  </code></pre>
  <p>impl.cpp:</p>
  <pre><code>#include &lt;detail/impl.hpp&gt;
  
  Impl::Impl() {/*maybe some custom implementation is necessary*/}
  </code></pre>
  <p>library.hpp:</p>
  <pre><code>#include &lt;detail/impl.hpp&gt;
  #include &lt;export.hpp&gt;
  
  class LIBRARY_EXPORT Library {
  public:
    //Library(); // ctor has to be declared and defined explicitly
  
  private:
    Impl i_;
  };
  </code></pre>
  <p>library.cpp:</p>
  <pre><code>#include &lt;library.hpp&gt;
  
  //Library::Library() = default; // ctor has to be declared and defined explicitly
  </code></pre>
  <p>test.cpp:</p>
  <pre><code>#include &lt;library.hpp&gt;
  
  int main() {
    Library{};
    return 0;
  }
  </code></pre>
  <p>Building this project leads to the following linker error:</p>
  <pre><code>/usr/bin/ld: CMakeFiles/test.dir/test/test.cpp.o: in function `Library::Library()':
  [..] undefined reference to `Impl::Impl()'
  </code></pre>
  <p>This error can be fixed by uncommenting the commented lines so that <code>Library</code> defines it's own default constructor. Just writing <code>Library() = default;</code> in the header or implementing it inline is not sufficient.</p>
  <p>Apparently special members being <em>implicitly defined</em> means they are not defined unless they are used. So <code>Library::Library()</code> is only generated as soon as <code>main()</code> tries to create an instance of <code>Library</code> where the symbols from <code>Impl</code> are not available. <code>Library::Library()</code> is <strong>not</strong> generated while compiling <code>library.cpp</code> (which makes kinda sense since it's empty).</p>
  <p>However, if i add a static method <code>foo()</code> to <code>Library</code> and implement it in <code>library.cpp</code> as <code>void Library::foo() { Library{}; }</code> the linker error persists. That means even though a default constructor of <code>Library</code> is generated as part of the library, it is not exported.</p>
  <p>Another observation which feels rather wrong is that removing the custom constructor from <code>Impl</code> also gets rid of the linker error. But then it is also possible to create an instance of <code>Impl</code> in <code>main()</code>, which should not be the case when <code>Impl</code> is not exported.</p>
  <p>Of course different compilers/linkers have different opinions about this. Compiling with GCC or clang throws the error mentioned above while MSVC just warns about <code>Impl</code> missing a dll-interface but everything works fine.</p>
  <p><strong>Is what I am observing intended behavior? If yes, is there any way to force the compiler to generate all special members while building the shared library apart from manually declaring them and then defining them as default?</strong></p>
  <hr />
  <p>Some additional information why I am looking for an alternative solution which are not essential to the core question:</p>
  <p>In the real project all 6 special members of several classes are affected which would lead to several hundred lines of code that basically say &quot;default&quot;.</p>
  <p>Additional problems occur if <code>Library</code> is a template class. Here it is also required to define the special member functions in a <code>.cpp</code> file. Only instantiating the template with all feasible types is not enough to generate special members:</p>
  <p>library_template.hpp:</p>
  <pre class="lang-cpp prettyprint-override"><code>template&lt;class T&gt; class Library {
  public:
    Library();
  
  private:
    Impl i_;
  };
  
  extern template class LIBRARY_EXPORT Library&lt;float&gt;;
  </code></pre>
  <p>library_template.cpp:</p>
  <pre><code>template&lt;class T&gt;
  Library&lt;T&gt;::Library() = default;
  
  template class Library&lt;float&gt;;
  </code></pre>
  <p>Which gets increasingly ugly when all I want to say is &quot;just default everything&quot;.</p>