31386

Boost Serialization - Exporting in multiple CPP files

Question:

The last days I have been struggeling with a Boost Serialization problem:

I am trying to serialize and deserialize multiple <strong>derived</strong> classes in multiple files. In order to keep it generic I have created template functions like:

template<typename T> void Helper::SaveToFile(T* data, std::string file) { std::ofstream ofs(file.c_str()); boost::archive::text_oarchive oa(ofs); oa << data; }

For serialization of derived classes to work, I need to use the Boost macro BOOST_CLASS_EXPORT. However, I cannot place this template method in a CPP file and with the macro in the header I get these annoying "duplicate init_guid" errors.

And even if I choose not to use a template method, I still get these errors due to the fact, that I have different serialize methods in different files and therefore exporting multiple times.

Does anyone have any tips on either howto make it work with template methods, or how to export classes in multiple CPP files?

I have already tried splitting BOOST_CLASS_EXPORT into BOOST_CLASS_EXPORT_KEY and BOOST_CLASS_EXPORT_IMPLEMENT, still leading to the same error. Also, I didnt really know where to put the BOOST_CLASS_EXPORT_IMPLEMENT macro when there is only a Header file for a specific class.

Answer1:

You're on the right track.

Splitting into BOOST_CLASS_EXPORT_KEY and BOOST_CLASS_EXPORT_IMPLEMENT is indeed the key to the solution.

As with all C++ symbols with external linkage, you

<ul><li>can put <em>declarations</em> in some shared location (like the header file)</li> <li>must put <em>definitions</em> in a <em>single</em> translation unit, so that only one linker input contains a definition.</li> </ul>

In this case, simply include BOOST_CLASS_EXPORT_IMPLEMENT in at most one (statically) linked translation unit (think: cpp file).

See for background:

<ul><li><a href="https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work/6264256#6264256" rel="nofollow">How does the compilation/linking process work?</a></li> <li><a href="https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix" rel="nofollow">What is an undefined reference/unresolved external symbol error and how do I fix it?</a></li> </ul>

Recommend

  • code guards fail
  • Rewriting a slow SQL (sub) query in JOIN
  • Regex collating symbols
  • Construct object from boost serialization archive
  • Remove standard english language stop words in Stanford Topic Modeling Toolbox
  • Storing the IV with the ciphertext Crypto++ CBC AES encryption
  • Linked List From Text File
  • What is that meaning of $testModule =~ s@/@::@ig; in Perl?
  • c++ - properly writing std::string to binary file
  • Retrieve Greek letters from MSSQL database with PHP
  • AWK how records and fields are executed and read
  • How to insert a different delimiter in between two columns in shell
  • fstream get(char*, int) how to operate empty line?
  • How to stop propagating declarations through hierarchical includes?
  • Boost Fusion: validate adapted struct member ordering at compile time
  • SerialForms.pas(17): W1010 Method 'Create' hides virtual method of base type 'TCompon
  • Can I put condition in emacs lisp macro?
  • Interrupting boost thread
  • GCC error: Cannot apply offsetof to member function MyClass::MyFunction
  • How to make a post call to a Web Api Action?
  • Complex Object won't return when using 'include' syntax in WCF with Entity Version 6
  • SOCKS in C/C++ or another language?
  • how to know the last row filled in vba (excel)? [duplicate]
  • C++ Boost ptr_map serialization error
  • Jackson Java 8 DateTime serialisation
  • Team Foundation Server 2015 vs. Bitbucket Server (Stash)
  • C++ and JS RegEx equivalent of \\p{L}
  • How can I emulate a recursive type definition in C++?
  • Fat binaries in iOS
  • VBA Dir function not working on Excel 2010
  • Get data from AJAX - How to
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Comma separated Values
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • Turn off referential integrity in Derby? is it possible?
  • How to load view controller without button in storyboard?