Boost C++ Libraries: Ticket #5368: Item for "Tips and Tricks" list in dokumentation https://svn.boost.org/trac10/ticket/5368 <p> How to handle BOOST_CLASS_EXPORT_KEY/BOOST_CLASS_EXPORT_IMPLEMENT in several static libraries and avoid linking errors? </p> <h2 class="section" id="MakeHeaderArchive.h">Make Header Archive.h</h2> <p> Make a Header "Archive.h". Include Everything for your serialization here. Example: </p> <div class="wikipage" style="font-size: 80%"><p> Code highlighting: : </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#ifndef __Archive_h__</span> <span class="cp">#define __Archive_h__</span> <span class="c1">// STL Archive + stuff</span> <span class="cp">#include</span> <span class="cpf">&lt;boost/serialization/export.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/serialization/base_object.hpp&gt; </span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/serialization/list.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/serialization/map.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/serialization/vector.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/serialization/shared_ptr.hpp&gt;</span><span class="cp"></span> <span class="c1">// IMPORTANT: Archive Headers at last</span> <span class="c1">// include headers that implement a archive in xml format</span> <span class="cp">#include</span> <span class="cpf">&lt;boost/archive/archive_exception.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/archive/xml_oarchive.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;boost/archive/xml_iarchive.hpp&gt;</span><span class="cp"></span> <span class="k">typedef</span> <span class="n">boost</span><span class="o">::</span><span class="n">archive</span><span class="o">::</span><span class="n">xml_oarchive</span> <span class="n">oarchive</span><span class="p">;</span> <span class="k">typedef</span> <span class="n">boost</span><span class="o">::</span><span class="n">archive</span><span class="o">::</span><span class="n">xml_iarchive</span> <span class="n">iarchive</span><span class="p">;</span> <span class="cp">#endif </span><span class="c1">// __Archive_h__</span> </pre></div></div></div><h2 class="section" id="Defineyourclassesasfollowing">Define your classes as following</h2> <p> Myclass.h </p> <div class="wikipage" style="font-size: 80%"><p> Code highlighting: : </p> <div class="wiki-code"><div class="code"><pre> <span class="cp">#include</span> <span class="cpf">&quot;Archive.h&quot;</span><span class="cp"></span> <span class="k">class</span> <span class="nc">Myclass</span> <span class="p">{</span> <span class="k">public</span><span class="o">:</span> <span class="n">Myclass</span><span class="p">();</span> <span class="o">~</span><span class="n">Myclass</span><span class="p">();</span> <span class="kt">double</span> <span class="n">md_data</span><span class="p">;</span> <span class="k">private</span><span class="o">:</span> <span class="k">friend</span> <span class="k">class</span> <span class="nc">boost</span><span class="o">::</span><span class="n">serialization</span><span class="o">::</span><span class="n">access</span><span class="p">;</span> <span class="c1">// When the class Archive corresponds to an output archive, the</span> <span class="c1">// &amp; operator is defined similar to &lt;&lt;. Likewise, when the class Archive</span> <span class="c1">// is a type of input archive the &amp; operator is defined similar to &gt;&gt;.</span> <span class="k">template</span><span class="o">&lt;</span><span class="k">class</span> <span class="nc">Archive</span><span class="o">&gt;</span> <span class="kt">void</span> <span class="n">serialize</span><span class="p">(</span><span class="n">Archive</span> <span class="o">&amp;</span> <span class="n">ar</span><span class="p">,</span> <span class="k">const</span> <span class="kt">unsigned</span> <span class="kt">int</span> <span class="n">version</span><span class="p">)</span> <span class="p">{</span> <span class="n">ar</span> <span class="o">&amp;</span> <span class="n">BOOST_SERIALIZATION_NVP</span><span class="p">(</span><span class="n">md_data</span><span class="p">;);</span> <span class="p">}</span> <span class="p">};</span> <span class="n">BOOST_CLASS_VERSION</span><span class="p">(</span><span class="n">Myclass</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="n">BOOST_CLASS_EXPORT_KEY</span><span class="p">(</span><span class="n">Myclass</span><span class="p">)</span> </pre></div></div></div><h2 class="section" id="Implementationfileofyourclass">Implementationfile of your class</h2> <p> Myclass.cpp </p> <div class="wikipage" style="font-size: 80%"><p> Code highlighting: : </p> <div class="wiki-code"><div class="code"><pre> <span class="n">Myclass</span><span class="o">::</span><span class="n">Myclass</span><span class="p">()</span> <span class="p">{</span> <span class="p">}</span> <span class="n">Myclass</span><span class="o">::~</span><span class="n">Myclass</span><span class="p">()</span> <span class="p">{</span> <span class="p">}</span> <span class="n">BOOST_CLASS_EXPORT_IMPLEMENT</span><span class="p">(</span><span class="n">Myclass</span><span class="p">)</span> </pre></div></div></div><h2 class="section" id="Conclusion">Conclusion</h2> <p> Thats the way i made it working in my project with VC10 and boost 1.46.1 . My project consists of several static libraries which have dependencies under each other. Main dependency is the library which contains the serializable classes. </p> <p> Hopefully this will help some people :-) </p> <p> Best Regards </p> <p> Georg </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5368 Trac 1.4.3 Aaron Barany <akb825@…> Fri, 25 Mar 2011 06:25:04 GMT <link>https://svn.boost.org/trac10/ticket/5368#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5368#comment:1</guid> <description> <p> Agreed, I needed to do this for our usage of serialization as well. </p> </description> <category>Ticket</category> </item> <item> <author>alvarolb@…</author> <pubDate>Wed, 20 Mar 2013 14:59:09 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5368#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5368#comment:2</guid> <description> <p> two days with linker problems! finally I can get it working! thx!! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Tue, 04 Feb 2014 02:03:55 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5368#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5368#comment:3</guid> <description> <p> I don't get this - I need a better explanation of the problem. I see that some other users have found this useful. Is this information not already in the documentation? Is there but confusing. Need a more specific explanation. </p> <p> Robert Ramey </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Thu, 06 Feb 2014 21:57:59 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5368#comment:4 https://svn.boost.org/trac10/ticket/5368#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> Ticket schorsch_76@… Sat, 08 Feb 2014 07:13:08 GMT <link>https://svn.boost.org/trac10/ticket/5368#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5368#comment:5</guid> <description> <p> Hello Robert, </p> <p> Yes, that is already in the documentation, but at that time i was relativly new to boost::serialization and it was not easy to get a working solution for me, so i added this ticket to boost to optimize the documentation for a starting point to explain to a new user the usage of BOOST_CLASS_EXPORT_KEY/BOOST_CLASS_EXPORT_IMPLEMENT. </p> <p> The problem were at start, as pointed out by comment 1 and 2, the linker problems. It's not a bug of the library, but of its usage of course. This simple example showed how to stuff it together to work around these linker problems. For you as inventor/developer of boost::serialization it's absolutly clear, but not for all of us ;) What's hard, is that the order of the include files are important. </p> <p> Thats my point. </p> <p> Bye Georg </p> </description> <category>Ticket</category> </item> </channel> </rss>